Hi,
I have a text file with the following formating:
+
12
12
I am making a program that reads the file and takes the file name that the user entered and conctenate the .out to the new file and output the results for example 12+12=24 so 24 should go in the new file. I am getting the segmentaion fault when I try to scan the name of the file from the user.
My code:
Code:
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(int argc,char**argv){
char c[10]; /* declare a char array */
FILE *file; /* declare a FILE pointer */
char *filename;
printf("Please enter the file name: ");
scanf("%s",&filename);
char *cc;
char *dd=".txt";
cc=strcat(filename, dd); // the file name
file = fopen(cc, "r");
/* open a text file for reading */
if(file==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
printf("File opened successfully. Contents:\n\n");
while(fgets(c, 10, file)!=NULL) {
/* keep looping until NULL pointer... */
printf("String: %s", c);
/* print the file one line at a time */
}
printf("\n\nNow closing file...\n");
fclose(file);
return 0;
}
}