Thread: Copying a string, into a string array.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    20

    Copying a string, into a string array.

    I'm having trouble taking a string, fed from a text file, and changing that string into an array of strings.

    My goal is, everytime i hit a '\n' in the string, it creates another string in the string array.
    I'm doing this because I'm trying to break a text file down, and be able to reference each one of its lines individually.

    For example, if i had a text file, as follows, and I read the whole thing into one (1) string,

    It seems today
    That all you see
    Is violence in movies
    And sex on tv

    and I wanted to print line 3 I'd just do
    Code:
    printf("%s", linearray[2]);
    Hopefully the follow code will give you a good idea of what I'm trying to accomplish, and maybe you can help me figure out why i cant get it to work.

    Code:
       for (x = 0, y = 0, z = 0; buffer[x] != EOF; x++)
       {
          if (buffer[x] == '\n')
          {
             y = 0;
             z++;
          } //end if
          else
          {
             linearray[z][y] = buffer[x];
             y++;
          } //end else
       } //end for loop
    I'm pretty sure the error is in my for loop here, if looks ok to all of you, I'll post the whole program so far, and let you analyze it all.

    I appreciate your help, everyone has been a big help, and my learning curve would be way down without you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your string will never contain EOF. You should be stopping on \0 (the null character). Now then, what is 'linearray'? A two dimensional array? An array of pointers to allocated space? What exactly? You could also simply read a line at a time with something like fgets and then use something like strcpy to copy from the buffer into your line.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    20
    If you were to copy the contents of a text file into a string, i've been using fread(), then would it not copy EOF to the last element of the string?

    My line array is designed as such,

    Code:
    char linearray[4][100];
    ...
    //put info into linearray[][]
    ...
    printf("%s", linearray[2]);
    its set up in such a way that i can printf any element i want, like the example above, and i get a string equivilant to an entire line of a text file. It uses the first dimension as the line number assignment, and the second dimension to actually hold the string.

    Now if fgets() already reads a text file one line at a time, maybe I'm working myself to death for no reason. If that is its intended use, let me know, and I'll research that instead of trying to debug this program.

    Thanks for your help.
    Michael

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Gotta love signatures - see mine for using fgets!

    Edit - note the title of the link as well.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you were to copy the contents of a text file into a string, i've been using fread(), then would it not copy EOF to the last element of the string?
    EOF is not a character. So you probably won't have it in your string.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Naturally you meant to say "So you definitely won't have it in your string."


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM