Searching text file

This is a discussion on Searching text file within the C Programming forums, part of the General Programming Boards category; Post the code you're trying to get working. Crystal ball is in the shop!...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post the code you're trying to get working. Crystal ball is in the shop!

  2. #17
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Code:
    FILE* output_file;
    output_file = fopen("pathToFile", "w");
    
    FILE* file;
    file = fopen("pathToFile", "r");
    
    char line[256];
    char term[12] = "TARGET";
    char data1[256];
    char data2[256];
    
    if (file != NULL)
    {
        while (fgets(line, sizeof(line), file))
        {
            if (strstr(line,term) != NULL)        //finds TARGET
            {
                fgets(line, sizeof(line), file);   
                fgets(data1, sizeof(data1), file);        //recommended code
                fprintf(output_file, data1, sizeof(data1)); //trying* to write
            }
        }
    }
    
    fclose(file);

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Unfortunately, I have already erased your data file from my computer. Can you post a small example of the data file that is failing with your code?

    Your fprintf() line of code should be:

    fprintf(output_file, "%s\n", data1);

    you have it confused with the format for fgets().

    You are getting no warnings or errors from this?

  4. #19
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    the data i am searching for will look something like this:

    TARGET
    1 4237568 -24001356 e236484865 1235.438975
    2 65.34576 4798722 u394623 .234568 23468769

    there is always a main name (TARGET) followed by 2 lines of data labeled 1 and 2. also there are hundreds line sets just like this before and after but all with a different main name.

    also for some reason it is no longer printing the target name to the file either. i will go back and see how i messed that up

    i was not getting any errors as it was but it is still not working. thank you for fixing my confusion there though!

    from recent testing. i am completely unable to write anything to the output file and cannot find how the code i provided earlier prevents that.
    Last edited by InicDominus; 09-27-2012 at 12:23 PM.

  5. #20
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Just used stdout to test and was able to get everything working fine. thanks again for all your help!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with searching text file
    By zacharyrs in forum C Programming
    Replies: 30
    Last Post: 12-01-2009, 02:13 PM
  2. Please Help in searching value in text file
    By david-_-zhang in forum C Programming
    Replies: 1
    Last Post: 02-10-2009, 02:10 AM
  3. searching text in a file
    By spveer in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 04:21 AM
  4. Searching a VERY large text file
    By Tankndozer in forum C Programming
    Replies: 4
    Last Post: 07-29-2004, 02:45 AM
  5. Searching A Text File?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 04:57 AM

Tags for this Thread


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