Split string in sequential file to add a tabstop between the strings...

This is a discussion on Split string in sequential file to add a tabstop between the strings... within the C Programming forums, part of the General Programming Boards category; Code: do: try to read some stuff if there is some stuff, then: split the string display the resulting strings ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Code:
    do:
       try to read some stuff
       if there is some stuff, then:
          split the string
          display the resulting strings
    while there is some stuff.
    Something like this.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to stop RIGHT AWAY. Once you type "char *gpa=", it's too late -- that is not right away any more. You cannot try to process the data if it isn't there.

  3. #18
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Quote Originally Posted by tabstop View Post
    You have to stop RIGHT AWAY. Once you type "char *gpa=", it's too late -- that is not right away any more. You cannot try to process the data if it isn't there.
    Guys I appreciate all the help. I have tried the if statement with !=NULL and ==NULL in it's current location and still getting segmentation fault. I am pulling my hair out with this one.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 100
    
    int main()
    {
        FILE *fP;
        char str[N], *c;
        /*char *gpa = strchr(str, ' ');*/
    
    
        if ((fP = fopen("names.dat", "r")) == NULL)
        {
            printf("%s not opened", "names.dat");
            exit(EXIT_FAILURE);
        }
        do {
            c = fgets(str, N ,fP);
            if (c != NULL);
            char *gpa = strchr(str, ' ');
            *gpa++ = '\0';
            printf("%s\t%s\n", str, gpa);
    
    
    
    
            }
        while (c != NULL);
    
        fclose(fP);
    
        return 0;
    
    }
    I really do not know what to do here. Again, guys I truely appreciate your time with me.

  4. #19
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
        do {
            c = fgets(str, N ,fP);
            if (c != NULL)
            {
              char *gpa = strchr(str, ' ');
              *gpa++ = '\0';
              printf("%s\t%s\n", str, gpa);
            }
          }while (c != NULL);
    Or more simply:
    Code:
    while(fgets(str, N, fP))
    {
      char *gpa = strchr(str, ' ');
      *gpa++ = '\0';
      printf("%s\t%s\n", str, gpa);
    }
    If you understand what you're doing, you're not learning anything.

  5. #20
    Registered User
    Join Date
    Jul 2010
    Posts
    177

    Talking

    Quote Originally Posted by itsme86 View Post
    Code:
        do {
            c = fgets(str, N ,fP);
            if (c != NULL)
            {
              char *gpa = strchr(str, ' ');
              *gpa++ = '\0';
              printf("%s\t%s\n", str, gpa);
            }
          }while (c != NULL);
    Or more simply:
    Code:
    while(fgets(str, N, fP))
    {
      char *gpa = strchr(str, ' ');
      *gpa++ = '\0';
      printf("%s\t%s\n", str, gpa);
    }
    Oh thank GAWD! or should I say all of you! Thank you very much!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. String as Resource in txt file
    By KeithS in forum Windows Programming
    Replies: 15
    Last Post: 08-29-2009, 12:04 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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