Thread: file

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    file

    could anyone point what went wrong with the code.
    I am just trying to display the contents of the file poem.txt on the screen.
    # include<stdio.h>
    void main()
    {
    FILE*fp;
    char s[80];
    fp=fopen("poem.txt","r");
    if(fp==NULL)
    {
    puts("cannot open the file");
    exit();
    }
    while(fgets(s,79,fp)!=NULL)
    printf("%s",s);
    fclose(fp);
    getch();
    }
    compiler is not giving any error,but still i am not getting the output.

  2. #2
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Works fine on mine, although I had to include the stdlib and conio librarys to make it work.

    As a future note, void main() is largely regarded as bad, main should return a value.

    What happened when you ran the code?
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    No, int main ( void ).

    >exit();
    exit must have an argument of 0, EXIT_FAILURE, or EXIT_SUCCESS to be portable. No argument is an error. You also haven't included stdlib.h.

    >getch();
    Not portable and you didn't include conio.h.

    Here are my corrections, I switched getchar with getch for portability reasons. If you want to change back do not forget to include conio.h.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      FILE*fp;
      char s[80];
      fp=fopen("poem.txt","r");
      if(fp==NULL)
      {
        puts("cannot open the file");
        exit(0);
      }
      while(fgets(s,79,fp)!=NULL)
        printf("%s",s);
      fclose(fp);
      getchar();
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char s[80];
    >>while(fgets(s,79,fp)!=NULL)
    Aren't you wasting a byte of the buffer?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    As those files get larger, you might also consider something like.

    Code:
    int rows;
    
    ....
    
    rows = 0;
    while(fgets(s,79,fp)!=NULL)
    {
        if(rows == 22)
        {
             printf("\npress any key to continue.\n");
             getchar();
             rows = 0;
        }
         else
             printf("%s",s);
      
        rows++;
    }
    fclose(fp);
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Remove the else

  7. #7
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    yikes, force of habit.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  8. #8
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    question....

    is this how they go around between rows of a text file ?
    do i have to loop 100 times if i want to go to the 100th line ?
    or is there a better way ?
    Last edited by moonwalker; 08-20-2002 at 11:20 AM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: question....

    Originally posted by moonwalker
    is this how they go around between rows of a text file ?
    do i have to loop 100 times if i want to go to the 100th line ?
    or is there a better way ?
    If you know how long each "line" is then you can seek to the correct location, otherwise you're stuck with looping through the input counting the lines.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    30

    Wink

    Hi,

    Try this code. It uses fread instead of fgets(). It gets the actual file size for you, so you can read in any text file in any size.

    Hope it helps! -


    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>

    int main(void)
    {
    off_t fsize = 0;

    FILE*fp;
    fp=fopen("poem.txt","r");
    if(fp==NULL)
    {
    puts("cannot open the file");
    exit(0);
    }

    struct stat attributes; struct stat *attributes_ptr;
    attributes_ptr = &attributes;
    stat("poem.txt",attributes_ptr);

    fsize = attributes_ptr -> st_size;
    char s[fsize - 1];


    fread(s,fsize,1,fp);
    printf("%s",s);
    return 0;
    }
    Best Regards,

    Bill

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Bill 101
    Hi,

    Try this code. It uses fread instead of fgets(). It gets the actual file size for you, so you can read in any text file in any size.

    fsize = attributes_ptr -> st_size;
    char s[fsize - 1];


    fread(s,fsize,1,fp);
    printf("%s",s);
    return 0;
    }
    This is not valid C code. In C, you cannot declare variables in the middle of the code block. You must do so at the beginning. Furthermore, this may or may not work on all compilers--actually, it will not work on all compilers--unless they conform to C99 specifications, due to the fact that you're using a variable sized array.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM