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; Hey everyone, I have the following code and trying to figure out a way to split the string, "Brown 3.75, ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    150

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

    Hey everyone, I have the following code and trying to figure out a way to split the string, "Brown 3.75, Jones 4.00, etc. and then add a tabstop between them for printing. Is there a simple command or way to do this? Thanks in advance!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 100
    
    int main()
    {
        FILE *fP;
        char str[N], *c;
    
    
        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)
    
                printf("%s\n", str);
            }
        while (c != NULL);
    
        fclose(fP);
    
        return 0;
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,151
    You could use strchr():
    Code:
    char *column2 = strchr(str, ' ');
    if(column2)
      *column2++ = '\0';
    else
      column2 = str + strlen(str);
    printf("%s\t%s\n", str, column2);
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    *scanf with formatspecifier %s will split for you:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 100
    
    int main()
    {
        FILE *fP;
        char str[N], *c;
    
    
        if ((fP = fopen("names.dat", "r")) == NULL)
        {
            printf("%s not opened", "names.dat");
            exit(EXIT_FAILURE);
        }
    
        while( 1==fscanf(fP,"%99s",str) )
          printf("%s\t",str);
    
        fclose(fP);
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    150
    Quote Originally Posted by BillyTKid View Post
    *scanf with formatspecifier %s will split for you:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 100
    
    int main()
    {
        FILE *fP;
        char str[N], *c;
    
    
        if ((fP = fopen("names.dat", "r")) == NULL)
        {
            printf("%s not opened", "names.dat");
            exit(EXIT_FAILURE);
        }
    
        while( 1==fscanf(fP,"%99s",str) )
          printf("%s\t",str);
    
        fclose(fP);
    
        return 0;
    }
    Thanks, but I have to use fgets. That was my first thought as well.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,612
    If you have to use fgets, then use sscanf on the string returned!

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    150
    Quote Originally Posted by rags_to_riches View Post
    If you have to use fgets, then use sscanf on the string returned!
    Now I am getting just one line of my .dat file and it happens to be the last one. Did I miss something?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    You're probably overwriting each time. Either print it out inside the loop, or save it somewhere.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    150
    Quote Originally Posted by BillyTKid View Post
    *scanf with formatspecifier %s will split for you:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 100
    
    int main()
    {
        FILE *fP;
        char str[N], *c;
    
    
        if ((fP = fopen("names.dat", "r")) == NULL)
        {
            printf("%s not opened", "names.dat");
            exit(EXIT_FAILURE);
        }
    
        while( 1==fscanf(fP,"%99s",str) )
          printf("%s\t",str);
    
        fclose(fP);
    
        return 0;
    }
    I really like what you did here, but can you tell me how I would get this print out?
    Brown 3.75
    Jones 4.00

    Basically I need to have a "name <tab> GPA" then a new line with the same.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,151
    Dude, the strchr() solution is faster/cleaner. If you omit the error checking just do this:
    Code:
    char *gpa = strchr(str, ' ');
    *gpa++ = '\0';
    printf("%s\t%s\n", str, gpa);
    Is there a reason you don't want to use it?
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    150
    Quote Originally Posted by itsme86 View Post
    Dude, the strchr() solution is faster/cleaner. If you omit the error checking just do this:
    Code:
    char *gpa = strchr(str, ' ');
    *gpa++ = '\0';
    printf("%s\t%s\n", str, gpa);
    Is there a reason you don't want to use it?
    Not at all, I am trying that as we speak. Just trying to understand it that is all. I really appreciate your help. Thank you!

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    150
    Quote Originally Posted by csharp100 View Post
    Not at all, I am trying that as we speak. Just trying to understand it that is all. I really appreciate your help. Thank you!
    Works good till a point, it prints out the output but then it stops working or other words the program does not exit on its own. I get a windows error and I am using codeblocks with the gcc compiler. Just Tried it on our unix system and I am getting a segmentation fault. Here is my code.
    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);
            char *gpa = strchr(str, ' ');
            *gpa++ = '\0';
            printf("%s\t%s\n", str, gpa);
    
    
    
    
            }
        while (c != NULL);
    
        fclose(fP);
    
        return 0;
    
    }
    Last edited by csharp100; 12-14-2010 at 04:17 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    When c is NULL you need to stop RIGHT NOW, not "after this loop is finished".

  13. #13
    Registered User
    Join Date
    Jul 2010
    Posts
    150
    Quote Originally Posted by tabstop View Post
    When c is NULL you need to stop RIGHT NOW, not "after this loop is finished".
    Could you be a bit more specific? I placed an if statement within the do/while loop after the print statement:
    Code:
    if (c = NULL)
    break:
    What it did was only print out one line and tat was the first line of my .dat file.

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,897
    Assignment is not equality:

    if(c == NULL) /* is it NULL? */

    if(c = NULL) /* assign NULL to C */

    Normally an IDE would warn if you use assignment in a boolean context. Turn up your warning level!
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

  15. #15
    Registered User
    Join Date
    Jul 2010
    Posts
    150
    Quote Originally Posted by whiteflags View Post
    Assignment is not equality:

    if(c == NULL) /* is it NULL? */

    if(c = NULL) /* assign NULL to C */

    Normally an IDE would warn if you use assignment in a boolean context. Turn up your warning level!
    Dang! Another segmentation fault. The latest code:
    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);
            char *gpa = strchr(str, ' ');
            *gpa++ = '\0';
            printf("%s\t%s\n", str, gpa);
            if (c == NULL)
            break;
    
    
    
            }
        while (c != NULL);
    
        fclose(fP);
    
        return 0;
    
    }

Page 1 of 2 12 LastLast
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