Thread: C program to find a line in a file and print it

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's go back to what you wrote in post #1:
    Quote Originally Posted by ArakelTheDragon
    Example:
    search for String: 0x22 0x023 0x24
    if String is found on line 9
    print the whole line 9, or print until you find a custom terminating character like "0x9999 "

    result must be: print 0x22 0x23 0x24 0x25 0x26 0x27 0x28.
    From the phrase 'custom terminating character like "0x9999 "', we can infer that "0x22 0x023 0x24" isn't a null-terminated string of length 15; rather it is a sequence of three bytes such that sequences of this kind could possibly contain embedded null characters, and be terminated by some "custom terminating character". This is very important because when we say "string" in C, by default we are talking about a null terminated string, i.e., contiguous sequences of characters up to the first null character.

    Consequently, your whole approach must change. You're dealing with a "binary file", not a text file, and with arbitrary sequences of bytes, not with strings. So strictly speaking, your file open mode should contain a 'b' (even though it may not be required), and you should be using file I/O functions that operate on bytes, not on strings (hence no fgets or puts), and you certainly cannot use strstr or strcmp because they assume null terminated strings as input.

    Or, perhaps your example is technically wrong, e.g., it should not have been:
    search for String: 0x22 0x023 0x24
    but rather:
    search for String: ""#;"
    and the "custom terminating character" terminates the sequence to find, not the string. If so, then my analysis does not apply, and indeed you can use functions that operate on strings.
    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

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe you should upload an example of this "database.cfc" files so we can see what it is you're trying to parse.
    Because it seems now far from clear that we're talking about any kind of regular text file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Thanks to everyone for their time and effort, I know its hard to code, but even harder to debug someone else's code.

    Final working version:
    Code:
                                                        // Built-in library includes //
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
                                                        // Custom includes //
    #include "Y:/Other/Library_OS/Functions.h"          // List of the functions.
    
    int main (void)
    {
                                                        // Local variables //
        char caLineStorage[1024];                       // Declare storage for 1 line  of type char.
        FILE *fpFilePointer;                            // Declare a pointer to a type file.
    
        fpFilePointer=fopen("Database.cfc","r");        // Open the file for reading.
    
                                                        // File opening error block.
        if(fpFilePointer == NULL) {
            perror("Unable to open file!");
            exit(1);
        }
                                                        // Processing //
        while (fgets(caLineStorage, sizeof (caLineStorage), fpFilePointer) != NULL)
            if (strstr(caLineStorage, "0x29") != NULL)
                puts (caLineStorage);
    
    
        fclose(fpFilePointer);                          // All files must be closed.
    
        return 0;
    }

  4. #19
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    1 more update, I wrote a similar function and tested it on Windows XP and Windows 10 Enterprise and on XP 32bit SP2 there is no problem, everything works correctly, but on Windows 10 enterprise the opening and writing to a file is not working. I used the exact same portable codeblocks.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the exact code that you tested? What is the test input, expected output, and actual output?
    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

  6. #21
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ArakelTheDragon View Post
    1 more update, I wrote a similar function and tested it on Windows XP and Windows 10 Enterprise and on XP 32bit SP2 there is no problem, everything works correctly, but on Windows 10 enterprise the opening and writing to a file is not working. I used the exact same portable codeblocks.
    Sounds like a possible Anti-virus software issue.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #22
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Thats ok, the thread is becoming too big anyway. I will solve this one by myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-02-2017, 04:00 PM
  2. Reading from a file and print in a different line
    By Giwrgows x in forum C Programming
    Replies: 3
    Last Post: 12-25-2015, 08:03 AM
  3. trying to print a line from a text file
    By kryonik in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2006, 09:14 PM
  4. print line by line from a file
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 01:36 PM

Tags for this Thread