Thread: Help on a simple "opening file" program :/

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Question Help on a simple "opening file" program :/

    Hello people i need your help:

    I got this code

    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? :/

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    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?
    Last edited by sepuku; 12-14-2010 at 05:09 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sepuku View Post
    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.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    So what must i change to put the output for me?
    Last edited by sepuku; 12-14-2010 at 05:22 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    For me is necessary to print the content of the text file!Is there a way to do it?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM