Thread: splitting a string

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    23

    splitting a string

    Hi,

    I need to split the input i receive from my file into two parts.

    File looks like this

    customerNo <space> typeofrequest

    eg

    1122 deposit

    how do i get the customer number as one string and the request in another?.

    I can do it in Java, but because c doesnt have checks like variablename.equals();

    i'm at a loss..

    Thanks.

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    look up strtok()

    if you cant find it...

    http://www.mkssoftware.com/docs/man3/strtok.3.asp
    Last edited by ventolin; 05-20-2004 at 01:12 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's easier than strtok. Read the entire string using something like fgets, and then simply sscanf the buffer into two variables.

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

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I can do it in Java, but because c doesnt have checks like variablename.equals();
    C has a standard library just like Java. In <string.h> there's a function called strcmp that performs the equivalent operation as x.equals(y);. But because there isn't a native substring function, you would be better off using quzah's suggestion.
    My best code is written with the delete key.

  5. #5
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Heres my version:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    
    int main()
    {
    	char buff[1024], result[1024], result1[1024];
    	FILE *file;
    	if ((file = fopen("/Source/file.txt", "r")) == NULL) {
    		fprintf(stderr, "fopen: Failed.\n");
    		exit(1);
    	}
    	if (fgets(buff, sizeof(buff), file) == NULL) {
    		fprintf(stderr, "fgets: Failed.\n");
    		exit(1);
    	}
    	sscanf(buff, "%s %s", &result, &result1);
    	fprintf(stdout, "Customer #: %s\nType: %s\n", result, result1);
    	if (fclose(file) == EOF) {
    		fprintf(stderr, "flcose: Failed.\n");
    		exit(1);
    	}
    	return 0;
    I wonder, 'sscanf()' suffers the same problems as 'scanf()'?
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I wonder, 'sscanf()' suffers the same problems as 'scanf()'?
    sscanf behaves the same way as scanf, yes. The biggest problem with scanf is that it works directly with stdin and doesn't mix well with other input functions. The second biggest problem with scanf is that it's very difficult to recover from effectively if an error occurs. sscanf solves the first problem because the "stream" is actually a string acquired elsehow. The second problem is solved by having the "stream" in memory. A string in memory is much easier to work with than an input stream when it comes to error handling.
    My best code is written with the delete key.

  7. #7
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Quote Originally Posted by Prelude
    >I wonder, 'sscanf()' suffers the same problems as 'scanf()'?
    sscanf behaves the same way as scanf, yes. The biggest problem with scanf is that it works directly with stdin and doesn't mix well with other input functions. The second biggest problem with scanf is that it's very difficult to recover from effectively if an error occurs. sscanf solves the first problem because the "stream" is actually a string acquired elsehow. The second problem is solved by having the "stream" in memory. A string in memory is much easier to work with than an input stream when it comes to error handling.

    Ahhh, I see. We just have to hope the programmer can handle their OWN, input. ; )

    Thanks....
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String splitting algorithm help
    By (TNT) in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2007, 11:28 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM