Searching for char in text file

This is a discussion on Searching for char in text file within the C Programming forums, part of the General Programming Boards category; I have variable char that holds number wich presents id of exam. Now i need to open my text file ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    42

    Searching for char in text file

    I have variable char that holds number wich presents id of exam. Now i need to open my text file and search for that number and if it is found programm needs to get name of that exam, and maximum number of points.

    Text looks something like this:

    5 1 Lab 01
    17 2 Lecture(31.10.)
    ...

    where 5 is id exam, 1 is max num of points and Lab 01 is name of exam.

    So far i got this:

    Code:
    char *nazivIspita;
        char tmp;
        int line_num=1;
        FILE *fin=fopen("Ispiti.txt", "r");
    
        while(fscanf(fin, "%d", &tmp)) {
              if(tmp==s.sifraIsp) {
                     printf("A match found\n");
              }
         }
    
        fclose(fin);

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    What is s.sifraIsp?

    Why you use %d to read into a char? Also, how much better would it be to write your code in English.....
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Oh sorry, i forgot to translate it. My mistake.

    Translated code:

    Code:
        char *examName;
        char tmp;
        FILE *fin=fopen("Exams.txt", "r");
    
    
        while(fscanf(fin, "%d", &tmp)) {
            if(tmp==s.examID) {
                        printf("A match found!\n");    
                       }
        }
    
    
        fclose(fin);
    s.examID is structure in which is written exam ID and it's type is char(it has to be) so that's why I use char.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Nice! Try using %c instead of %d. Also I would also check if fin is different from NULL, in order to be sure that the file is opened ok.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,119
    Quote Originally Posted by yahzee View Post
    Code:
          while(fscanf(fin, "%d", &tmp)) {
            if(tmp==s.examID) {
                        printf("A match found!\n");    
                       }
        }
    s.examID is structure in which is written exam ID and it's type is char(it has to be) so that's why I use char.
    char is simply a small integer, so there's no problem with using it together with "%d", as long as the values are within the range [CHAR_MIN, CHAR_MAX].

    However the problem is you're reading only integers, and you said your input has also course names. Therefore your fscanf will stop as soon as it hits a non-integer and never proceed.

    As a different approach why not read an entire line (since your input data looks to be line-based). Then use sscanf on the line. If there is a match, then print the line.

    You can read a line of input with fscanf.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    I have tried all of that and all sort of things but i just don't know how to take that first number and compare it with s.examID. And if it's match exame name should be written in char *examName. If it's not match he should search next line and so on until he founds match.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by c99tutorial View Post
    char is simply a small integer, so there's no problem with using it together with "%d", as long as the values are within the range [CHAR_MIN, CHAR_MAX].
    .
    It is within that range(it's in range[5,22])
    I will try your advice, but i am not sure how to write that code.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    No luck. I used fscanf to read entire line and then sscanf to read number from that string.
    Now it looks like this:

    Code:
    char exameName[300];
        char tmp[300];
        char num;
        int maxPoints;
        FILE *fin=fopen("Exams.txt", "r");
    
    
        while(fscanf(fin, "%s", &tmp)!=EOF) {
            sscanf(tmp, "%d %d", &num, &maxPoints);
            if(num==s.examID)
                printf("Ok");
        }
    But the problem is that i don't know how to take first two numbers, first number should represent exam id and it's purpose is just to compare it that's the right exame, and if it is the right one i should read maximum number of point(second number in line) and exame name which i need to write another txt file.
    For now i am just printing ok as a test.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    But the problem is that i don't know how to take first two numbers, first number should represent exam id and it's purpose is just to compare it that's the right exame, and if it is the right one i should read maximum number of point(second number in line) and exame name which i need to write another txt file.
    For now i am just printing ok as a test.[/QUOTE]

    I'd suggest you use fgets() to take in one whole line of text. Then use strstr() to see if the word "Lab" is in that line of text.

    if "Lab" is found, then sscanf(buffer, etc.) to get the char you want. I do wonder if having the char as a number -- ie., using %d, isn't a better way to go.

    For instance, what if the "char" digit is really TWO digits: say 29, instead? That becomes a bit sticky, imo.

    It's a number, so I'd read it as a number. Change your struct as needed.

    You will need a char buffer[100], and you will need to include string.h to use strstr(buffer, "Lab").

    Anyway, think that over. fscanf() is for strictly formatted data, and while it can be very useful, it's nowhere near as easy to work with as fgets(buffer, sizeof(buffer), fp).

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    I am already trying with fgets, but thanks for notice anyway. I can't search for Lab because i don't know that information yet, i know only first number (examID). And when i read first line with fgets and then try to print result, it prints me first line 5 times?!?!

    Code:
    char examName[50];
        char tmp[100];
        char num;
        int maxPoint;
        FILE *fin=fopen("Exams.txt", "r");
    
    
        fgets(tmp, sizeof(tmp), fin);
        printf("%s", tmp);
    
    
        fclose(fin);

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Then why did you show this as the text file content?
    Code:
    5 1 Lab 01
    17 2 Lecture(31.10.)
    The problem is, looking for just a 5 could give you more than one line of text, couldn't it? You need something to ID the right line of text, and I'm not sure 5 by itself, is the best answer.

    Or is it OK as an answer to look for?

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    The problem is, looking for just a 5 could give you more than one line of text, couldn't it? You need something to ID the right line of text, and I'm not sure 5 by itself, is the best answer.

    Or is it OK as an answer to look for?
    I should explain the programs purpose, because i think i misled you with wrong description. The program searches user for input of students code. Then program uses students code to search .dat file for match, when it finds match it returns number which represents exam ID and number of points student have in that exam. Program stores values in struct, the number of points and exam ID have to be type of char. The .txt file contains 18 lines of text. Format of each line is:

    'exam ID' 'max num of points' 'exam name' (for example: '5' '1' 'Lab 01')

    Given the information that we only know exam ID, we need to search .txt file for match of s.examID and first number in each line. Then from that line we need to read max num of points in that exam and name of that exame. We need that information to write new .txt file wich include max num of points, exam name, exam id and num of points student has.

    Sorry for bad English. If something is understandable feel free to ask.

    Now my problem is that i scan first line in order to scan her as a string but when i try to print her just for test purposes it prints first line 5 times. I tried just to scan first char into char tmp; and then print tmp, it also prints first character 5 times(in this case number 5).

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by yahzee View Post
    Now my problem is that i scan first line in order to scan her as a string but when i try to print her just for test purposes it prints first line 5 times. I tried just to scan first char into char tmp; and then print tmp, it also prints first character 5 times(in this case number 5).
    Can you post a small, compilable program which shows this behaviour?

    Bye, Andreas

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by AndiPersti View Post
    Can you post a small, compilable program which shows this behaviour?
    Ofcourse, here it is:

    Code:
    char examName[300];
        char tmp[100];
        char num;
        char maxPoint;
        int i;
        FILE *fin=fopen("Exams.txt", "r");
    
    
        fgets(tmp, sizeof(char)*100, fin);
               printf("%s", tmp);
    
    
    
    
        fclose(fin);
    Other parts of program are working just fine. So far this is the dead end for me.

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    That's not a compilable program which shows your problem.

    If I use the example exams file from your post #1:
    Code:
    $ cat Exams.txt
    5 1 Lab 01
    17 2 Lecture(31.10.)
    and turn your code snippet into a working program:
    Code:
    #include <stdio.h>
      
    int main(void)
    {
        char tmp[100];
        FILE *fin=fopen("Exams.txt", "r");
        fgets(tmp, sizeof(char)*100, fin);
        printf("%s", tmp);
        fclose(fin);
        return 0;
    }
    I get the following output:
    Code:
    $ ./foo
    5 1 Lab 01
    That's why I've asked for a small, compilable program which shows that you get the output 5 times, because as you can see the code snippet you have posted just outputs the line once.

    Bye, Andreas

Page 1 of 2 12 LastLast
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, 02: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, 02: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

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