Thread: Read first line of a file and store in a variable

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    14

    Read first line of a file and store in a variable

    Dear friends,

    I have a file named "in.txt" with following data:

    10
    20
    30
    40


    and now I want to store the first line of this file which is "10" in a variable of type integer. would you please help with this? so that after running the program lets say variable "nFirst" equals to "10".

    Regrads.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Which part of the problem is the problem? Do you know how to do input in general? Do you know how to open a file? What?

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Please google around and read up on fgets() and fopen(). This may point you in the right direction to learn how to open up a file and get its contents.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    u can wrote it like this
    tag
    #include<stdio.h>
    main()
    {
    FILE *o;
    o=fopen("in.txt","r");
    if((o=fopen("in.txt","r"))!=NULL)
    {
    int x;
    fscanf(o,"%d",&x);
    printf("nfirst = %d",x);
    fclose(o);
    }
    }
    tag

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @mustafa:

    It's better to give them a hint, and let them work at it for awhile. If our plumbers can't work with toilets, I don't want them to be plumbers.

    Instead of tag, try:

    [code1]
    Code:
    Your code in here
    [/code1]

    Just leave off the 1, to make it work.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Instead of saying "tag", you can use the real code tags, [code] [/code]

    Also, doesn't it seem a bit strange to name an input file "o"? . . . .

    [edit] @Adak: try [noparse][code] [/code][/noparse] to display what you intend. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 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. Read SPECIFIC line from text file
    By 13373ar5 in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2004, 05:14 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM