How to get only a certain portion of the string

This is a discussion on How to get only a certain portion of the string within the C Programming forums, part of the General Programming Boards category; Hello there all. I have a small program I wrote (with help of some here). If takes a number from ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    How to get only a certain portion of the string

    Hello there all. I have a small program I wrote (with help of some here). If takes a number from the command line and then parses a file based on it. For example:

    If you gave the number 3 and had a file:

    1231
    23412 112312
    aaa23231 234234

    it would result in:

    1
    12 112312
    23231 234234

    cutting off the first 3 characters. I do that with the following code:

    (b is the command line number)

    Code:
           while(fgets(buf, sizeof(buf), infile))
                 fprintf(outfile, "%s", buf + b);

    Now, i want to hardcode in a number to say, only grab that many characters. So with the same example as above if i hardcoded "6" i would get:

    1231
    23412 112312
    aaa23231 234234

    would be:

    1
    12 112
    23231


    only taking the first 6 characters from the string.


    i would imagine i'd need something similiar to this, but i can't figure out what to do:


    Code:
           while(fgets(buf, sizeof(buf), infile))
                 fprintf(outfile, "%s" + 6, buf + b);
    Any help would be appreciated. Thanks!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    fprintf(outfile, "%.6s\n", buf + b);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by Dave_Sinkula
    Code:
    fprintf(outfile, "%.6s\n", buf + b);

    Dave,
    Thank you, but that turned input like this (using 8 as the command line number, and 6 as the hardcode number):
    Code:
    1234 ZZ 123456    XXXX.XXXX.XXAAAAAAXXXXXX.XXXXX.XXXXXBBB
    into output like this:

    Code:
    123456
    AAAAAA
    BBB
    instead of:

    Code:
    123456

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    And what is the size that buf is declared?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by Dave_Sinkula
    And what is the size that buf is declared?
    25

    Code:
    void main(int arcg, char *argv[])
    {
    
        char buf[25];
        char file1[20] = " ";
    	 char file2[20] = " ";
        char a[20] = " ";
        int b;
    	 FILE *infile, *outfile;
    
    
        if (arcg != 4) {printf("\cut [column] [file to scan] [file to write to]");
       return;
       }
    
     strcpy(a, argv[1]);
     strcpy(file1, argv[2]);
     strcpy(file2, argv[3]);
    
     b = atoi (a);
    
     if ((infile = fopen(file1,"r")) == NULL)
         { printf("The file that you want\n");
           printf("scanned is not found.  Try again.");
    
            return;
            }
    
    
      if ((outfile  = fopen(file2,"w")) == NULL){ fclose(infile); return;}
    
    		   while(fgets(buf, sizeof(buf), infile))
                 fprintf(outfile, "%.6s\n", buf + b);
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    main returns int.

    Code:
    printf("\cut [column] ...
    Invalid escape sequence . . . did you mean \n?

    Do you include <stdio.h>?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >25

    Well, trying to put 57 characters into a 25-char buffer might have something to do with it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    That's because you're reading 24 chars at a time (fgets() reads 1 less than the size you supply to leave room for the terminating '\0'). And every time you read 24 chars you're printing the buffer starting at index b and printing 6 characters.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Just make the buffer bigger (like 80 or 200) and see what happens.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by dwks
    main returns int.

    Code:
    printf("\cut [column] ...
    Invalid escape sequence . . . did you mean \n?

    Do you include <stdio.h>?

    Yes stdio.h is included.

    \cut - cut is the name of the program, just prompting the user on what the sequence on the command line should be.

    on error prints:

    cut [column] [file to scan] [file to write to]

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by itsme86
    That's because you're reading 24 chars at a time (fgets() reads 1 less than the size you supply to leave room for the terminating '\0'). And every time you read 24 chars you're printing the buffer starting at index b and printing 6 characters.
    Understood, didn't realize it. I'll bump the buffer up to 100.

    Thank you!

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    \cut - cut is the name of the program, just prompting the user on what the sequence on the command line should be.

    on error prints:

    cut [column] [file to scan] [file to write to]
    No, it doesn't. It prints "\c" (whatever that is) followed by "ut". You probably meant "\ncut".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by dwks
    Just make the buffer bigger (like 80 or 200) and see what happens.

    That's exactly what I did.


    Thank you all for taking the time to explain.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Did you fix the '\c'?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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