Thread: [Help] Text file as a 1D or 2D array in C

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    [Help] Text file as a 1D or 2D array in C

    Hello, my problem is a problem of a bigger program. In this part of a program I have to load a text file which looks like something this::


    sta1,sta2,sta3,q3,sta5
    a,b,c,d
    sta2,sta5,q3
    sta1
    sta1,a->sta2
    sta2,b->sta5
    ...


    Program has to read a file not knowing the number of lines (I made a function for that) and not knowing what's exactly in a file (example: q3,q1,sta2,q4,sta1 instead of the first line of the file)

    Is there a way to load that text file as a large 2D array or as multiple 1D arrays (one line = one array)? Something like this:



    sta1,sta2,sta3,q3,sta5\0
    a,b,c,d\0\0\0\0\0\0\0\0
    sta2,sta5,q3\0\0\0\0\0
    sta1\0\0\0\0\0\0\0\0\0
    sta1,a->sta2\0\0\0\0\0
    st2,b->sta5\0\0\0\0\0

    etc.

    Now, I tell the program to print mat[2][4] and it will print ,
    or (second case, 1D arrays) to print r3[6] and it will print -

    (I don't need the printing option in my program, I need an different elements from that large array)


    I hope you understand what I want to do. I've no idea how to make this or is it even possible. Please, help me. Thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Does the text file have any nulls in it? That is, is it just plain text?

    If so, you could probably load it and parse it as a single string...
    (FWIW... a "string" in C can be up to 2gB in length.)

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    Does the text file have any nulls in it? That is, is it just plain text?

    If so, you could probably load it and parse it as a single string...
    (FWIW... a "string" in C can be up to 2gB in length.)
    It's just a plain text.

    I have to write a program which simulates a work of an machine which goes from one state to another based on information from that text file.

    sta1,sta2,sta3,q3,sta5 - all possible states
    a,b,c,d - characters which changes states (baesd on the lower lines)
    sta2,sta5,q3 - if the final stete one of the this states it's successful
    sta1 - first state (start state)
    sta1,a->sta2 - based on this lines, program changes states (if it is in sta1 and receives a it will go to sta2)
    sta2,b->sta5
    ...

    There's another text file from which it reads lines of characters (also unknown)
    Last edited by Zallman; 03-13-2011 at 09:35 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes well, good luck with that... I don't see any obvious answers.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    Yes well, good luck with that... I don't see any obvious answers.


    Here's what I have to do:

    here's the example files:

    Definition file:

    q0,q1,q2,q3,q4
    0,1
    q1,q3
    q0
    q0,$->q1,q3
    q1,1->q1
    q1,0->q2
    q2,1->q2
    q2,0->q1
    q3,0->q3
    q3,1->q4
    q4,0->q4
    q4,1->q3
    Characters

    1
    0
    11
    00
    1011
    0010
    1010
    1101
    1001
    10110
    100011
    00110
    011110
    0110111
    Not successful lines of characters are: 1011,0010,1101,100011
    The rest is successful. Program has to print which lines are successful and which are not.

    it reads character after character (in this case a number 0 or 1) and go through states and if it ends in an acceptable state it is successful.

    As you can see these files are different than mine, but the structure is the same and the program must read it anyway.

  6. #6

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by rags_to_riches View Post
    What's wrong with that? Multiple places, higher the chances that someone will help.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Zallman View Post
    What's wrong with that? Multiple places, higher the chances that someone will help.
    Just the opposite... the more you crosspost the more we tire of our time being used up while you listen to someone else's advice...

    In any case, I still don't see how you're going to solve the problem.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Zallman
    What's wrong with that? Multiple places, higher the chances that someone will help.
    • Duplication of effort.
    • Lack of respect to those who would answer your question.
    • Lower chance of coming to a resolution when answers differ (or even directly contradict).

    So, pose your question to exactly one community. Only if you fail to get a satisfactory answer after some time then pose your question to another community, providing them with a reference to what answers you received previously.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    • Duplication of effort.
    • Lack of respect to those who would answer your question.
    • Lower chance of coming to a resolution when answers differ (or even directly contradict).

    So, pose your question to exactly one community. Only if you fail to get a satisfactory answer after some time then pose your question to another community, providing them with a reference to what answers you received previously.
    Sorry if I have insulted you, I am just trying to find the answer to my problem and I don't have much time so I asked the same question on multiple forums.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Here's my answer to selfish cross-posting bums.

    > What's wrong with that? Multiple places, higher the chances that someone will help.
    Oh yeah, well read this then and understand!
    http://www.catb.org/esr/faqs/smart-questions.html#forum
    http://www.catb.org/esr/faqs/smart-q...ns.html#urgent
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading file characters into an int array
    By spongefreddie in forum C Programming
    Replies: 9
    Last Post: 10-28-2010, 12:23 PM
  2. Copying content of file into a 2D array
    By trueman1991 in forum C Programming
    Replies: 10
    Last Post: 12-16-2009, 12:42 AM
  3. reading a string from a text file into a 2d array
    By duelord in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2009, 07:29 AM
  4. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03:50 PM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM