Thread: File processing: How to read a part of a text file?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    9

    File processing: How to read a part of a text file?

    So I was wondering, how can I read only a part of a text file.
    There are modes read, write, append, and binary mode. Which one do I use? and how?

    Like for example:
    Filename:
    Code:
     Sample.txt
    Content:
    Code:
    Jack and Jill 
    went up the hill
    
    to get a bucket of water
    they ran down the hill
    
    and fell together.
    so how do I read only
    Code:
    to get a bucket of water
    Do I use? : (This simply prints ALL the texts in Sample.txt)
    Code:
    FILE *fp;
    char ch;
    if( (fp = fopen("Sample.txt", "r")) == NULL)
         fprintf("stderr, "ERROR: Sample.txt does not exist.\n");
    while( (fscanf(fp, "%c", &ch)) == 1)
         fprintf(stdout, "%c", ch);
    fclose(fp);

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The file is text, so use text mode. And since you're reading it (looking for a specific line), you want read mode, so leave fopen as it is, with the "r" parameter. Congrats on checking fopen for failure, however, if it fails, you need to print an error then exit. No point in continuing if your file isn't open. Also, remove the extraneous quote from before stderr.

    Don't use %c in fscanf, that's for individual characters. I suggest using fgets to read an entire line, and strcmp to compare that line to the one you're after:
    Code:
    char buf[256];
    while (fgets(...) != NULL) {
        if (strcmp...) {
            printf(...);  // printf is the same as fprintf to stdout
        }
    }

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Thats a great idea!
    I've got an error trying that though.

    Is this how you use it?
    Code:
    char ch[300];
    while (fgets(ch, 300, fp) != NULL)
        {
            p("c\n");
    if(strcmp(ch, "to get a bucket of Water") == 0)
                printf("%s\n", ch);
        }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That is how you use it. A quick note, I suggest:
    Code:
    #define MAX_LINE_LENGTH    300
    char ch[MAX_LINE_LENGTH+1];  // +1 for the null terminator
    fgets(ch, sizeof(ch), fp)
    That way, if you need to make ch bigger, you only need to change the definition of MAX_LINE_LENGTH, and the rest of your code can stay the way it is.

    Your problem is probably because fgets reads in the new line ('\n') at the end of each line, thus ch is "to get a bucket of Water\n", not "to get a bucket of Water". You can get rid of it like so:
    Code:
    int len;
     len = strlen(ch);
    if (ch[len - 1] == '\n') {
        ch[len - 1] = '\0';
    }

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Got it, thanks so much for you help. Happy programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read part of text in file
    By eldy in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2011, 12:54 PM
  2. read part of png file
    By fleurdelys in forum Game Programming
    Replies: 8
    Last Post: 10-23-2011, 04:19 AM
  3. Read part of file
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 01:51 PM
  4. Reading Part Of A Text File
    By Okiesmokie in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2002, 01:59 PM
  5. How do i delete a certain part of a text file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2001, 06:00 PM