0

What ever could be the problem with it?

#include <stdio.h>
#include <string.h>
#define SIZE 19

void filePrint(char fileName[]);

int main (void)
{
   char fileRead[SIZE];

   filePrint(fileRead);

   return 0;
}

void filePrint(char fileName[])
{
   FILE *inp;
   int input_status = 0;
   char readFile[SIZE];

   inp = fopen(fileName, "r");

   printf("\nEnter a file to print:\n");

   input_status = fscanf(inp, "%s", readFile);

   while (input_status != EOF)  
   {
      printf("%s\n", readFile);
      input_status = fscanf(inp, "%s", readFile);
   }

   fclose(inp);
}
1
  • 2
    You will learn more, and get better answers, if you can minimize the amount of code you post that produces the same problem. If you just dump all your code and say "Solve my problem," we won't be terribly motivated to fix your code, but if you post small samples and say, "Why does this not do what I expected?" we will generally be happy to explain it to you. Commented Feb 18, 2010 at 4:38

6 Answers 6

7

I think you should go back and read a chapter on File I/O.

Run through the code you wrote in your mind, and out loud.

You're trying to open a file, stored in the fileName string, but the string hasn't been initialized to anything that is valid (for fopen). fopen returns a NULL pointer if it cannot open a file. You cannot use this NULL pointer to read from. Also, if you're using fscanf to read from the file you just opened, a user cannot type anything.

Sign up to request clarification or add additional context in comments.

Comments

3

Among other things, you never actually specify the file to read from?

2 Comments

Actually, the segfault happens when he tries to read from the filehandle, which is dereferencing a NULL pointer. But the pointer is NULL for the reason you state, so +1 for being helpful instead of pedantic.
If fileRead doesn't happen to contain any NUL characters, the segfault may happen before fopen() returns.
2

When you call fopen(fileName, "r");, fileName has not been filled with a filename. It's an uninitialized array.

Comments

1

Looks like you never put anything into fileRead[] in main, before you fed it to filePrint(), which gave it to fopen(). I.e., "uninitialized data"

Comments

1

If you mentioned the filename in fileRead[] array also,you will get the segmentation fault. Because you specified the array size is 19.You should specify the large array size in fileRead[] array like 1024.

Comments

1

You should give the filename for the function.

You pass the character array without value. So that time, the value of the array is null. In the fopen function you are tried to open the file.

So the fopen function return null value. If the fopen function open the file successfully it will return the correct file pointer. Else it will return the null, The error will be stored in the errno.

Using the null pointer you can't read.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.