Reading data from files

This is a discussion on Reading data from files within the C Programming forums, part of the General Programming Boards category; I have a text file with the following lines: 1 | Matrix | 1 | Action | 3 2 | ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    6

    Reading data from files

    I have a text file with the following lines:

    1 | Matrix | 1 | Action | 3
    2 | X-men 2 | 1 | Action | 2
    .
    .
    .
    id | movie name | rental type | genre | stock

    I want to read the data from the text file and store 1 into an id variable, Matrix into a movie string etc etc.

    Any ideas on how to accomplish this?

    I have thought of a few ways to implement this such as by using fgetc and manually checking every char for a '|' but what I would like to do is to use fscanf which would make the whole process much easier. Unfortunately fscanf would not be able to read X-men 2 as a single string.

    Thanks in advance.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Assuming that your string buffers are 101 characters in length:
    Code:
    fscanf("%d | %100[^|] | %d | %100[^|] | %d", &id, nameBuf, &type, genreBuf, &stock);
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    I would probably use fgets and strchr.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    6
    Edit: yeah it works... thanks alot man. Going to look up what the %[ specifier does exactly now. Mind explaining how it works in detail?

    thanks anyway.
    Last edited by ChwanRen; 05-06-2003 at 02:17 PM.

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >Mind explaining how it works in detail?
    Any book on C can tell you, but you can think of it as a simple regular expression. A character is read from the stream as long as it is inside the character set (inside the square brackets). The ^ symbol compliments the entire set so that it matches anything not in the set. So %100[^|] will read at most 100 characters or until a | is found.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  2. Problem with reading files
    By glopv2 in forum C Programming
    Replies: 3
    Last Post: 08-01-2007, 12:05 AM
  3. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 02:48 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 08:27 AM

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