Reading text file in c

This is a discussion on Reading text file in c within the C Programming forums, part of the General Programming Boards category; hey guy..I want know how to print certain line from the text file and certain part of the record for ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    Reading text file in c

    hey guy..I want know how to print certain line from the text file and certain part of the record for example

    123 John Smith 80
    222 chris brown 50
    325 christine 60
    I only want to print out the name from the record in the text file..
    for example when the user enter 222 for the id it should print out the id and the name ....

    OUTPUT :
    222 chris brown

    anyone please help ....thx

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Basic outline:

    Read contents of file into an array of structs.
    Get id from user.
    Search for id in array of structs.
    Print into from array of structs.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Well, if user inputs "222", you copy that in temp string with extra space on end (with sprintf() or strcpy()/strcat()), so you have "222 " string .Then you scan each line in file and compare that line with temp string (with strncmp(), n=strlen(temp_string)). If there's a match, print it with last space replaced with 0 (find it with strrchr() and put there '\0').

    EDIT: or just strcat(" ") to inputted string if char array is large enough (i presume it is), so no need for temp string.
    Last edited by rasta_freak; 08-13-2008 at 10:28 PM.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Is this duplicate of some post discussed some time ago?

    fgets line

    sscanf(line, "%d %[^0-9]%d", ...)

    something like that?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    Is this duplicate of some post discussed some time ago?

    fgets line

    sscanf(line, "%d %[^0-9]%d", ...)

    something like that?
    I think it's a different poster.

    What confuses me is that some of the names in the list has First and Last name, the last line however contains only a first name [presumably first name at least]. In general, we would like to see the same content on each line. Assuming the content is incorrect in the sample, the simple solution is to read the first number, first name, last name and last number into variables using fscanf(). If the first number matches the user input, then print the data for that line. If the lines are indeed containing either one or two names, the problem becomes much harder. As I expect this to be a beginner task in file handling, I think I'm right in saying that the file should ALWAYS have first and last name.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm wrong - it is the same user who posted both threads - obviously couldn't find his/her way back to the original thread, so just posted again. Not to mention that there is a complete and as far as I can tell correct solution posted here, to the same questio by as far as I can tell, the same person (or two people with very similar names are posting the same question on different forums):
    http://www.daniweb.com/forums/thread139622.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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, 01:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM

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