Karker Carnesir
Karker Carnesir

Reputation: 75

Segmentation fault, scanf

We have an assignment which is to take characters from a file, shift it a given value to the right(it'll make sense in the code) and then store that new value in a new file, but I seem to be running into a segmentation fault, which as far as I know means I'm trying to access memory outside of the memory I have been allocated? I'm very new to C and I managed to debug this code up until this point and I honestly don't know where to go. I don't even quite understand what the issue is.

#include<stdio.h>
//Get Shift amount
//Get ifilename
//Get ofilename
//open them
//Get characters one at a time from input
//Process and shift by shift amount
int main()
{
    int i;//loop value
    char a[62]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//Variable with every value
    int sa = 0;//Store Shift value
    char ofile[30];//contain the name of output file
    char ifile[30];//contain name of input file
    char value;//Value to keep the current value in

    printf("How far in ascii values would you like to shift?\n");
    scanf("%i", sa);//Get shift
    printf("What is the name of your input file located in this directory?");
    scanf("%s", ifile);//get input name
    printf("What would you like to name your new file?\n Do note that it will overwrite the current file named this!");
    scanf("%s", ofile);//Get output name

    FILE *oIfile = fopen(ifile, "r"), *oOfile = fopen(ofile, "w");

    while(value = fscanf(oIfile, "%c", value) != EOF)//Check to ensure that you never reach the end of the file
    {
        for(i=0; i<62; i++)//loop through the list of all characters
        {
            if(value == a[i])//check to see if the value from the input file matches with which letter
            {
                value = a[i+sa] % 62;//incrase the value by the shift amount, check if its longer than 62, add remainder
                break;//break the for loop so we can restart the while loop with the next letter
            }
        }
        fprintf(oOfile, "%c");//print the new value to the output file
    }
    fclose(oIfile);//close input file
    fclose(oOfile);//close output file
}

is this issue due to my approach to scanf?

Upvotes: 1

Views: 350

Answers (2)

user2736738
user2736738

Reputation: 30906

Apart from passing the address to scanf (scanf("%i",&sa) which you would have to do (Also it would be correct to check the return value of it) - you need to correct a few things:-

  • It should be (value = fscanf(oIfile, "%c", value)) != EOF. != has higher precedence than = so this is needed to get the correct result.

  • Also a[(i+sa)%62]=... is the right way to do things. Because otherwise it will access array index out of bound for certain values of sa leading to undefined behavior.

  • fprintf(stream,"%c",charvariable) this would be the use for fprintf.

  • The value is over written with what is being returned by the value that fscanf returns. You should use other temporary variable or even better simply do like this while(fscanf(oIfile, "%c", value)!=EOF). But to have more checks you need to do somehting like

      int c;
      while((c=fscanf(oIfile, "%c", value))!=EOF)
    

You got segmentation fault here by passing the value of sa instead of its address in scanf.

Upvotes: 5

cleblanc
cleblanc

Reputation: 3688

scanf take an address as a parameter. Try this;

scanf("%i", &sa);//Get shift

Upvotes: 2

Related Questions