reading file delimited with '|'

This is a discussion on reading file delimited with '|' within the C Programming forums, part of the General Programming Boards category; Hi, I've a file as: 2 0 1 6|3 4 5 |1 1 6 7|1 0 5 6|0 2 3 ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    reading file delimited with '|'

    Hi,

    I've a file as:
    2 0 1 6|3 4 5 |1 1 6 7|1 0 5 6|0 2 3 8\n

    I want to read it as:
    2 0 1 6
    3 4 5
    .
    .
    ..till the end of line.

    Could anyone suggest what to use?

    Thanks,
    Angkar

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    fscanf() should work for you. Or maybe fgets() and strtok()/strchr().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps even fgets/strcspn (again).
    http://www.daniweb.com/code/snippet444.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    Yeah no problem you can even do this by simple looping and move to next part whenever you encounter a " | ".

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks a ton...

    Now I've a file as :

    ---------------- file begin

    File Name
    w3
    y88
    a86
    h2
    .
    .
    .
    **
    --------------file end

    I want to read the file in an array of strings S as:
    S[0] = "w3"
    S[1] ="y88"
    .
    .
    .
    so on.

    How can I do it?
    Appreciate your guidance.

    Thanks,
    Angkar

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Seems like an obvious candidate for fgets, since you're reading one line into each string.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. reading parts of a tab delimited file
    By AngKar in forum C Programming
    Replies: 8
    Last Post: 01-24-2006, 10:38 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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