Thread: string manipulation, strnspn() a text file

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    string manipulation, strnspn() a text file

    Code:
    /* Write a program that searches a text file for occurrences of a user-specified target string and then reports the line numbers where the target is found.  For example, if you search one of your C source code files for the string "printf()", the program should list all the lines where the printf() funcction is called by the program. */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define BUFSIZE 128
    
    int main( void )
    {
        char buf[BUFSIZE];
        char filename[60];
        FILE *fp;
        size_t loc;
        char string[80];
    
        if ( (fp = fopen("testfile.txt", "r")) == NULL)
        {
            fprintf(stderr, "Error opening file.");
            exit(1);
        }
    
        printf("Enter the string to be searched: ");
        fgets(string, 80, stdin);
        puts("\n");
    
        int count, line_num;
    
        count = 0;
        line_num = 0;
    
        while (!feof(fp))
        {
            line_num++;
            fgets(buf, BUFSIZE, fp);
            loc = strspn(string, buf);
            printf("%d %s", line_num, buf);
            if(feof(fp))
            {
                if ( loc == 0 )
                    printf("\nNo match was found.\n");
                else
                    printf("\nCharacters match up to postion %lu.\n", loc-1);
            }
        }
        
        fclose(fp);
        return(0);
    }
    text file, testfile.txt (for program):

    Code:
    apple banana mango strawberry Orange 100 200 300 +400 Watermelon
    well...i've spent a few hours on this code. I looked through my programming book at the string manipulation functions, not exactly sure what one to use for this exercise, but i chose strspn(). Also, I switched up the format of the source text file, including adding a list of words instead of a line of words separated by spaces. The program in the exercise should only display the words that are matching by fgets, or string.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    strspn is not the right function to use: the idea of strspn is that the second string argument contains a set of characters used to search for in the first argument. What you're trying to do, however, is to find one string in another, so you should use strstr instead.

    Furthermore, you should use the return value of fgets to control the loop. I would write the code according to pseudocode like this:
    Code:
    while read in line into buf using fgets
        increment line_num
        if string is found in buf according to strstr
            print line_num
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a string from a text file after a specific text
    By GypsyV3nom in forum C Programming
    Replies: 4
    Last Post: 06-22-2016, 01:11 PM
  2. Input/Output Text File Manipulation not working!
    By Frank1234 in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2013, 07:54 AM
  3. File and text manipulation
    By A.C Milan in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:10 PM
  4. c++ string creation and file manipulation
    By ccb056 in forum C++ Programming
    Replies: 24
    Last Post: 09-20-2003, 02:01 PM
  5. Text file manipulation problem
    By ferretman in forum C Programming
    Replies: 3
    Last Post: 12-20-2001, 02:40 AM

Tags for this Thread