Like Tree2Likes
  • 2 Post By laserlight

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

This is a discussion on [Help] Text file as a 1D or 2D array in C within the C Programming forums, part of the General Programming Boards category; Hello, my problem is a problem of a bigger program. In this part of a program I have to load ...

  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 10: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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655

  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
    22,256
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    32,821
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01: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, 04:50 PM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM

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