Thread: Character Arrays and Strings?

  1. #1
    Registered User
    Join Date
    Sep 2010

    Character Arrays and Strings?

    I'm really sorry, because I know this is probably a really easy question, but I pretty much suck at programming, and I'm working on code right now that is supposed to read the information in a text file, then print some of that information. The only problem for me is that it is a more advanced file than I'm used to reading, so I'm not really sure how to begin.

    The input file is:

    Last Name	First Name		Class Year	Hometown
    Allen 	        James		3	        Tampa, FL
    Jones	        Adam		2	        New York, NY
    Smith	        Stacey	        3	        Chicago, IL
    Beam            Sarah	        3	        Chicago, IL
    Oliver          Taylor          4	        Los Angeles, CA
    I want the user to be able to choose which line (1-5) prints the data, but like I said, I'm not sure how to begin.

    Again, I'm sorry that this is a pretty simple question, but any help would be greatly appreciated.

    Thank you!
    Last edited by ocswimfast; 09-15-2010 at 08:25 AM. Reason: formatting

  2. #2
    Registered User
    Join Date
    May 2010
    fopen(), fgets(), sscanf() from stdio.h
    You can google around and I'm pretty sure there're tons of examples how to use these functions.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Thanks, I looked for those and they helped a little bit.

    I think I'm confused about how to identify the name or value I want to print in my code; all of the examples I've seen, online and in my book, rely on a user input value, then using fgets to analyze the value. I need the user to tell me a line to print the information of, and that's what I'm more unsure about.

    Thanks again for any input.

  4. #4
    Registered User
    Join Date
    Sep 2010
    If the user gives you the line number, there is no problem - you just need to perform fgets() several times.

    If user needs to know data about fx. Sarah, well that will be more challenging In that case you will have to parse the line after getting it from file, then perform some checking.

  5. #5
    Join Date
    Dec 2007
    Each line represents a record. A record is best represented in code by a struct. A group of records is stored in an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 02:08 PM
  3. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  4. Adding character arrays to strings.
    By w00tw00tkab00t in forum C++ Programming
    Replies: 28
    Last Post: 02-06-2006, 07:03 PM
  5. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM

Tags for this Thread