-1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    struct info
    {
        char name[15];
        char surname[15];
        char gender[15];
        char education[15];

    } sem;

    FILE *fp=NULL;
    int i, a;
    char tmp[256] = {0x0};

    while(1)
    {
        printf("Enter the value\n");
        scanf("%d", &a);

        if((fp = fopen("info.txt", "r")) != NULL)
        {

            switch(a)        
            {               

                case 0:
                        exit(0);        

                case 1: 

                    for(i=0;!feof(fp);i++)
                    {
                        fscanf(fp, "%s %s %s %s", sem.name, sem.surname, sem.gender, sem.education);
                        printf("%s, %s, %s, %s\n",sem.name,sem.surname,sem.gender,sem.education);
                    }   

                    break;

                case 2:

                    while (fgets(tmp, sizeof(tmp), fp) != NULL)
                    {
                        if (strstr(tmp, "bachelors"))
                        {
                            /* Code works fine until this part */
                            fprintf(fp, "\n%s %s %s %s", sem.name, sem.surname, sem.gender, sem.education);                         
                        }   
                    }

                    break;

                default:    printf("Default statement");                                        
            }

            fclose(fp);

        }
    }
}

If anyone could point me out what im doing wrong, id be very greatful, I added a comment where code runs in to a problem and doesnt display anything. Basicly i have txt file. Program if user so desires needs to find lines in the file where "bachelor" is typed and give me back all of those lines.

Marty
  • 29
  • 5
  • `for(i=0;!feof(fp);i++) ...` is incorrect. Read more about the "EOF anti-pattern" here: http://stackoverflow.com/questions/5431941 and http://drpaulcarter.com/cs/common-c-errors.php#4.2 – Michael Burr Dec 12 '13 at 07:06

2 Answers2

2

You are opening your file in read mode (fp = fopen("info.txt", "r")) and trying to write in it using fprintf() which is not possible.

Use fp = fopen("info.txt", "r+") i.e read and write mode.

AJ.
  • 4,526
  • 5
  • 29
  • 41
  • Also you can't read then write (or vice versa) to the same `FILE*` without an intervening call to a file positioning function (or an `fflush()` in the case of write followed by read). – Michael Burr Dec 12 '13 at 07:02
  • @MichaelBurr That is correct. But op is using `switch()` and `fclose()` for handling the operation so that is fine. – AJ. Dec 12 '13 at 07:05
  • the problematic `fprintf()` is in a loop controlled by `fgets()` on the same `FILE*`. – Michael Burr Dec 12 '13 at 07:07
  • @MichaelBurr Oh yes, now I see. – AJ. Dec 12 '13 at 07:09
0

If you want to compare strings, you will have to use strcmp(), not an undefined function like "strstr". Also, strcmp returns 0 if two strings have same value. So you also have to check that the return value of strcmp() is zero or not.

Also as I replied to your question yesterday, fprintf() method appends the characters that you've passed as arguments to file. So, in your code, when you find string "bachelor", you just add same line at the end of the file. If you want to see those data in console, you can use printf() method.