0

I am writing using CodeBlocks, GCC and using C language. The code I have is as follows:

char word[50];
FILE *fn;
fn = open("word.txt", "rb");
if(fn == NULL) perror("File not opened");
while(!feof(fn))
{
    fscanf(fn, "%s", word);
}

The text file has only one word in it (at present that is "batman"). I have tried various versions of this code including the following:

char* word;
char = malloc(50);
FILE *fn;
fn = open("word.txt", "rb");
if(fn == NULL) perror("File not opened");
while(!feof(fn))
{
    fscanf(fn, "%s", &word);
}

I honestly don't know why (and why this should be SO difficult) but the error that CodeBlocks is giving me is the dreaded SIGSEV segmentation fault at the feof line. When I didn't do the while loop the fscanf would throw a Segmentation fault. Please help! I don't often work in C and I remember why now. Even the most simple of things can be SO difficult. Thanks in advance.

UPDATE: I have actually got this to work (the top one) by changing the open line to:

fn = fopen("C://Temp//word.txt", "rb");

How can this work with relative paths?

Daniel Casserly
  • 3,552
  • 2
  • 29
  • 60
  • `perror` just prints a message. It does not terminate the program. So if `fn` is null, bad things will happen. – Mat Nov 27 '12 at 21:57
  • 2
    If should be `fopen`, not `open` - and your compiler should be warning you about this - you are compiling with `gcc -Wall ...` I hope ? – Paul R Nov 27 '12 at 22:00
  • Yes sorry it is giving a warning. @Mat I have made the program return and it is saying that word.txt is not a valid file or directory even though there is a word.txt in the same directory as the .exe file that I am running - am i missing something? – Daniel Casserly Nov 27 '12 at 22:02
  • Don't ignore the compiler warnings - they are there for a good reason ! – Paul R Nov 27 '12 at 22:05
  • Your second code segment doesn't compile. Do you mean word = malloc(50) instead of char = malloc(50)? – Kev Nov 27 '12 at 22:05
  • Sorry guys, please see update. – Daniel Casserly Nov 27 '12 at 22:08
  • Sorry sorted. It's just that CodeBlocks is being a pain in the proverbial - the file is fine if you click it from within Windows. For some reason codeblocks was ignoring it... – Daniel Casserly Nov 27 '12 at 22:17

2 Answers2

2

Change

fn = open("word.txt", "rb");

to

fn = fopen("word.txt", "rb");

open returns an int, which is not a FILE*. feof and fscanf expect a FILE* not an int as its first parameter, that's the reason why feof() and, if you remove it, fscanf() segfaults.

And as others already noted, change fscanf(fn, "%s", &word) to fscanf(fn, "%s", word).

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
1

Assuming that word is a char * (your code is not clear), this instruction will bring you problems:

fscanf(fn, "%s", &word);

Just change it by:

fscanf(fn, "%s", word);

Also, I suggest you to use the fopen function instead of open, since fscanf works with descriptors provided by fopen.

Hernan Velasquez
  • 2,770
  • 14
  • 21