Help appreciated, storing contents of a file

This is a discussion on Help appreciated, storing contents of a file within the C Programming forums, part of the General Programming Boards category; I have an input file that contains the names of books, the file is entirely strings, some lines with spaces. ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    1

    Help appreciated, storing contents of a file

    I have an input file that contains the names of books, the file is entirely strings, some lines with spaces. I'm using a while !EOF loop and it works fine, inside the loop I'm using
    fgets to read the line/string into a buffer, from there I need to store the string in the buffer into a new string. I though sscanf would work untill it became obvious that whitespace was
    an issue. Can anyone throw out some tips or point me in the right direction for this? (Not looking for code, just a push in the right direction).
    Last edited by Wsorne; 05-11-2007 at 06:24 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The string.h include file will support strcpy(string1, string2), where both of these strings are names of char arrays.

    Of course, it's also easy to roll your own.

    Remember that a string is not just a bunch of char's in a row. It MUST have an end of string char at it's end to be treated properly (and considered elevated to a true string): '\0'.

    Code:
    while((c = string1[i]) != '\0')
       string2[i] = string1[i++];
    Might be a normal starting point if you wanted to not #include <string.h> and use strcpy().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. storing text file contents into a string
    By m.mixon in forum C Programming
    Replies: 4
    Last Post: 07-20-2006, 12:52 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

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