Thread: Issues with searching through a .mal file for specific string.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    15

    Issues with searching through a .mal file for specific string.

    So I have a file "input.mal" that I am searching through for specific words. The problem is that after using
    Code:
    while(fgets(line, sizeof line, inp2) != NULL)
    line gets some random quotations inbetween words. And this causes issues because I am using strtok to split up the line at commas, spaces, and tabs. So now these symbols appear as part of the word and cause strcmp to say 2 words arent the same.

    Say the line in the file is simply
    put avg

    when it gets read into line, I get some random stuff. Should I maybe be nulling line after every iteration of the loop? Here is a screen shot of what the debugger is showing. You can see that on line[0] and line[8] there is that "
    Issues with searching through a .mal file for specific string.-line-jpg

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How big is this file?

    If it's less than about 20megabytes, I'd consider loading it as one big string (using fread()) then searching it with strstr().

    I'm sure that would be a lot faster than mucking about with line by line stuff.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    The files are not very big at all. Maybe 500 lines max. I did do this with strstr and everything works fine. I was trying to redo it though because I am concerned that if for some reason there is avg2 on a line and I am searching for avg1, I dont want it to say that it was found when it was really something else.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by aw1742 View Post
    You can see that on line[0] and line[8] there is that "
    Nope. What I see there is '' (two single quotes), which looks a lot like " (one double quote).

    This is (partially) the fault of some poor thinking by the people who designed your IDE. The character is ASCII 10 (notice the numbers on the left? eg, right before that is ASCII 103, 'g'), aka the newline, represented in C (and most if not all other languages) as '\n'. Unfortunately, your IDE just uses '', as if it were an unprintable character. Send them a bug report -- honestly, that is a very dumb system.

    If you are not familiar with ASCII values:

    http://www.asciitable.com/
    Last edited by MK27; 11-11-2011 at 03:09 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    Thanks, I dont know why visual studio would be set up like that. Doesnt make much sense but at least I should be able to work around that.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aw1742 View Post
    The files are not very big at all. Maybe 500 lines max. I did do this with strstr and everything works fine. I was trying to redo it though because I am concerned that if for some reason there is avg2 on a line and I am searching for avg1, I dont want it to say that it was found when it was really something else.
    That might happen with avg12 but not avg2 ... One way is to be more specific... " avg1 " let it find the padding too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a specific string/word/etc in a text file?
    By zacharyrs in forum C Programming
    Replies: 5
    Last Post: 11-29-2009, 07:54 PM
  2. Searching for specific string using fstream
    By comwiz in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2007, 03:37 AM
  3. Searching for a list specific words in a string
    By shoobsie in forum C Programming
    Replies: 3
    Last Post: 04-27-2006, 06:04 AM
  4. searching for a specific item in a C++ file
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2002, 04:54 PM
  5. Searching for a String within a file
    By TidusBlue in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 07:29 PM