Thread: Searching for char in text file

  1. #16
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    I found mistake. It's not really a mistake, because program was working ok. It was my wrong assumption that something was wrong because function that calls this function called for it 5 times as it should. I forgot about that because i was only thinking about this function. Shame on me. Still i have problem with reading all the required informations from .txt file. Does anyone know how to search this text line by line until match is found on exam ID(first number in line), and then from that line how to take max num of points(second number in line) and exam name(rest of text in line).

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your English is OK, but -- parsing input files must be exact, or they're not going to work. So, please post up a sample input file, that is *exactly* what you will be working with. Yes, a few samples of BOTH files.

    In general, for line by line reading, you can't beat fgets(). For searching, it depends, but strstr or strchr, is usually the way I do it.

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    So you are able to read each line of the file using fgets(), right?

    Now you need to parse the line (extract the information you need). It looks like your text is formatted like: number (exam ID) followed by space followed by number (max points) followed by space followed by text (exam name). Thus sscanf() should work.

    For the numbers you can use the format specifier %d, but for the text you can't use %s because your text could include spaces. But as far as I understand it the exam name is everything which follows the two numbers. This seems to be the right job for %[. If you don't know this format specifier yet I recommend reading The power of scanf() - Cprogramming.com or the man page.

    Bye, Andreas

  4. #19
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Here is everything i wrote, and files wich my program uses:

    C program.zip

    I tried couple of tweeks within sscanf, but i failed to succed. I just don't know what's the reason.

  5. #20
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    I finally managed to solve it. Thanks for help everyone, you were very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

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