reading .txt file

This is a discussion on reading .txt file within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main() { FILE *pfile; char my[200]; pfile=fopen("my1.txt","r"); if(pfile==NULL) perror("error in opening file"); else { fgets(my,200,pfile); puts(my); fclose(pfile); ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    reading .txt file

    Code:
    #include<stdio.h>
    int main()
    {
    FILE *pfile;
    char my[200];
    pfile=fopen("my1.txt","r");
    if(pfile==NULL) perror("error in opening file");
    else {
    fgets(my,200,pfile);
    puts(my);
    fclose(pfile);
    }
    return(0);
    }
    i/p file is this
    30,25,25,301,301,301,295,294,291,289,288,288,285,2 67,244,211,142,93,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,2,1,1,1,940.0000,S
    P,86.4000,1,2,O ,HAWAII-NORTH AMERICA,HNL ,ORD ,HNL ,ORD ,,,2,0,UA,,2008-02-20,2008-02-20,2009-01-08,,O,,UA,,1,10079,0,36
    ,0,312,0,0,0,70,70,72,XA,0,0,0,0,0,0,0,0,0,0,0,189 9-12-31,,1,1899-12-31,15,0,89,0,0,0,1208.0000,782.0000,940.0000, 0.0000, 0.
    0000, 0.0000,282.0000,577.0000,419.0000,429.0000,669.000 0,616.0000,480.0000,380.0000,347.0000,305.0000,289 .0000,241.0000,306,
    0,11,0,82,88.7694,0,0,0, 0.0000,72.0000,2,0,6,499.0000, 0.0000,0,0,0,HAW,Hawaii,65.0000,, 0.0000,

    0/p i am getting is
    30,25,25,301,301,301,295,294,291,289,288,288,285,2 67,244,211,142,93,0,0,0,

    The above c binary is reading only first line.
    I want to get all lines in .txt file what the the problem in the code.please help me I am new to c programing.

    Thanks & Regards ,
    Vijay,

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you have to loop if you want all of the lines.
    Code:
    while(fgets(my, 200, pfile)) puts(my);

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    Meldreth got one problem

    Hi Meldreth,
    Thank you very much but its giving space after each line. Please help me in this.

    Thanks and Regards,
    Vijay

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    fgets preserves any new line character. use puts if there's no new line and fputs if there is.
    Code:
    while(fgets(my, 200, pfile))
    {
        if (!strchr(my, '\n')) puts(my);
        else fputs(my, stdout);
    }

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    Meldreth

    Thanks a lot.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Maye you want fread() and fwrite() instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. reading .txt file
    By cnewbee in forum C Programming
    Replies: 4
    Last Post: 03-12-2003, 10:12 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21