3

I am using CodeBlocks to create my program in C and I'm having trouble with the following issue.

I am trying to open and read from a .txt file and it works fine if I put it into the main function like this:

int main(int argc, char *argv[])
{

FILE *input;

input = fopen("file.txt", "r");

char singleLine[50];

while(!feof(input)){

fgets(singleLine, 50, input);
puts(singleLine);
}

However, if I set the name of the file "file.txt" as an argument in CodeBlock using the "Set Program's Arguments option, and then want to pass it into a function that would read from it like this:

void read(char *name){

File *input;

....
....

}

And call it like this:

int main(int argc, char *argv[])
{


read(&argv[1]);

}

It does not work and the program crashes.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Daeto
  • 440
  • 1
  • 6
  • 19

2 Answers2

3

If your function prototyope is

void read(char *name)

then you need to call it like

read(argv[1]);

because , argv[1] itself gives you a char *, not &argv[1].

FWIW, always check for the validity of argv[n] before using it directly.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Hey there and thanks for your reply!I definitely made a mistake there but it still does not work. Maybe something to do with the location of the file? – Daeto Nov 27 '15 at 16:11
  • @Daeto after checking `argc > 1` try printing the file name in `main() `and again in `read()`. You must also test `input == NULL` after attempting to open the file. Finally please don't use `read()` as a function name - it's already used for a library function. – Weather Vane Nov 27 '15 at 16:23
  • The printf function prints the name of the file both in main and in the read function so I guess passing the name of the file as an argument is not being an issue here. However, I added the condition to check whether opening the file was successful, it it turns out that it was not. Any idea why? The commands in the function look just like I described them above in main. – Daeto Nov 27 '15 at 16:30
  • @Daeto you are opening the file for reading, so it must it already exist, on a path that the program will look in. – Weather Vane Nov 27 '15 at 16:33
  • @Daeto you have shown code for what worked, but you didn't show the code for what did not work. – Weather Vane Nov 27 '15 at 16:35
  • Oh I think I just figured it out! Inside the function for reading, when I was trying to open the file instead of writing fopen(filename, "r"); I was writing fopen("filename", "r"); Now it works properly. So when passing the file as an argument, if I were to open it inside main function, I would then write fopen(argv[1], "r"); and I only put the name of the file into quotation marks when I know I want to open that one particular file, right? But the file in the argument can change. – Daeto Nov 27 '15 at 16:46
0

Test

read(&argv[0]);

and see what is in argv in console

printf(&argv[0]);
theWalker
  • 2,022
  • 2
  • 18
  • 27
  • 2
    I'm confused. Sourav said I should not be passing arguments into the read function like this. Also, argv[0] is the location of my program isn' t it? Why would I need that? If I print argv[1], it prints file.txt which is the name of the file located in the same directory as my program. So I guess it is really there. – Daeto Nov 27 '15 at 16:22
  • 1
    `printf(&argv[0]);` is really a _bad_ example, even ignoring the logic, whatsoever. – Sourav Ghosh Nov 27 '15 at 16:25