PDA

View Full Version : Help on a simple "opening file" program :/



sepuku
12-14-2010, 04:40 PM
Hello people i need your help:

I got this code


#include<stdio.h>

int main()
{ FILE *f;
f=fopen("animalist.txt","r");
getchar();

return 0;
}

My problem is that although animalist.txt is in the same folder my program runs i don't get the contents of my text and i just have to press ctrl+C to end my program.
Moreover,i want my program to ask me which path to navigate then give me an "ls" of the files that exist in the directory i chose and finaly open a file of my choice?Any ideas? :/

brewbuck
12-14-2010, 04:56 PM
You aren't reading anything from the file, and you're not printing anything to the screen. So how exactly did you expect that this would display a file?

sepuku
12-14-2010, 05:00 PM
So my program just opens the file ?This means i need an fprintf?"Read mode" is for reading the file right?so why it does not show the content?

tabstop
12-14-2010, 05:12 PM
So my program just opens the file ?This means i need an fprintf?"Read mode" is for reading the file right?so why it does not show the content?

It opens the file for the program. It doesn't open the file for you in nano or whatever.

sepuku
12-14-2010, 05:13 PM
So what must i change to put the output for me?

tabstop
12-14-2010, 05:18 PM
You don't. Your program processes the file and does whatever it's supposed to do with it. It will output whatever output is necessary to perform this task.

sepuku
12-14-2010, 05:21 PM
For me is necessary to print the content of the text file!Is there a way to do it?

tabstop
12-14-2010, 05:26 PM
Then your program should read the contents of the file and print the contents of the file. (You probably don't have to do the whole file at once; read a line, print a line, read a line, print a line, etc. should suffice.)