read string with fgets followed by another string needed to read

This is a discussion on read string with fgets followed by another string needed to read within the C Programming forums, part of the General Programming Boards category; Hey guys, another question that someone might know. I have to read a line containing three string sets needed for ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    read string with fgets followed by another string needed to read

    Hey guys, another question that someone might know. I have to read a line containing three string sets needed for data of my program. However, the different strings are seperated by different amounts of whitespaces /tabs and the middle string can actually consist of several "strings" as it is the boat name. I have no idea how to go about it, as I keep thinking that if I use fgets() and find the first string, I d loose the seconds string. --> since i dont know how many whitespaces are leading the first string and cant therefore set the chars to read for a particular purpose.
    Any help is really appreciated.
    Cheers,
    Johannes.

    Attached is a sample file we were given for the assignment.

    Cheers.
    Attached Files Attached Files
    • File Type: txt b2.txt (433 Bytes, 176 views)

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Try something like this...

    Code:
    int main()
    {
      char inputString[MAX_STRING_SIZE];
      char stringOne[MAX_STRING_SIZE];
      char stringTwo[MAX_STRING_SIZE];
      char stringThree[MAX_STRING_SIZE];
    
      /* read long string from file into inputString[] */
    
      /* search forward for start of first word (first non-white space) */
    
      /* search forward for end of first word (next white space) */
    
      /* copy data between the two points into stringOne */
    
      /* search back for end of 3rd word (last non-white space) */
    
      /* search back for start of 3rd word (next white space) */
    
      /* copy data between two points into stringThree */
    
      /* stringTwo lies between endStringOne and startStringThree */
      /* minus the white space - find start and end */
    
      /* copy data between two points into stringTwo */
    
      return 0;
    }
    I'll leave you to fill in the gaps
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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