String Compare and File Handling mixing together...
I am practicing my self to combine functions together like this.
so get this, is it possible to search record from the File using File Handling(fscanf)
and compare it from the User's input and display it...check out my program.

content of 2003.txt

1234 [tab] John [tab] Doend
3214 [tab] Jane [tab] Dredy
2104 [tab] Joey [tab] Drone

here is my program

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

main()
{
      int opt,opt1,a;
      int x;
      char fn[15],ln[15],sn[15],data[15],line[225];
      char locate[15];
      FILE *f03,*f04,*f05;
      FILE *s13,*s23,*s33;
      FILE *s14,*s24,*s34;
      FILE *s15,*s25,*s35;

      printf("\t\t\t\t*****MENU*****");
      printf("\n\n\n");
      printf("\t\t\t\t***STUDENT PROFILE***\n\n");

                  f03=fopen("2003.txt","r");
                  printf("2003 Student Enrollies\n\n");
                  printf("Student Number \t\tFirst Name \t\tLast Name\n\n");

                  while(!feof(f03))
                  {
                  fscanf(f03,"%s\t%s\t%s\n",&sn,&fn,&ln);
                  printf("\n%s\t\t\t%s\t\t\t%s",sn,fn,ln);
                  }
                  fclose(f03);

        printf("\n\nEnter Data to Display: ");
        scanf("%s",&data);
        system("cls");

                  if((strcmpi(data,"1234")==0)||(strcmpi(data,"John")==0)||(strcmpi(data,"Doend")==0))
                  {
                  s13=fopen("03/1234.txt","r");
                  while(fgets(line, sizeof(line), s13) != NULL)
                  { 
                                    printf("%s",line);
                  }
                  fclose(s13);
                  }      

                  else if((strcmpi(data,"3214")==0)||(strcmpi(data,"Jane")==0)||(strcmpi(data,"Dredy")==0))
                  {
                  s23=fopen("03/3214.txt","r");
                  while(fgets(line, sizeof(line), s23) != NULL)
                  { 
                                    printf("%s",line);
                  }
                  fclose(s23);
                  }       

                  else if((strcmpi(data,"2104")==0)||(strcmpi(data,"Joey")==0)||(strcmpi(data,"Drone")==0))
                  {
                  s33=fopen("03/2104.txt","r");
                  while(fgets(line, sizeof(line), s33) != NULL)
                  { 
                                    printf("%s",line);
                  }
                  fclose(s33);
                  }
                  else 
                  {
                       printf("\n\n\t\t\tNo record Exist\n\n\n");
                  }

      printf("\n\n");
      system("pause");

}

if you can see here:

if((strcmpi(data,"1234")==0)||(strcmpi(data,"John")==0)||(strcmpi(data,"Doend")==0))

I already inputed the '1234','John' and 'Doend'...hence the program is long and I may want add a EDITING function to my program.

My question is, can I change them into a single variable EACH?
example '1234' is variable 'sn', 'John' is variable 'fn' and 'Doend' is variable 'ln'
and I can still compare it and open the correct file for it.
the correct file to open for John is 03/1234.txt

if that can happen my program will be shorter right? less hassle for someone.

Thank you in advance to everyone willing to help me with this trouble and also to Daniweb.com \m/

Your program could be improved by better use of if statements and using only two FILE*pointers.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>


int main()
{
      int opt,opt1,a;
      int x, index;
      char fn[15],ln[15],sn[15],data[15],line[225];
      char locate[15];
      FILE *fp1, *fp2;
      char* filename;

      printf("\t\t\t\t*****MENU*****");
      printf("\n\n\n");
      printf("\t\t\t\t***STUDENT PROFILE***\n\n");

      fp1=fopen("2003.txt","r");
      printf("2003 Student Enrollies\n\n");
      printf("Student Number \t\tFirst Name \t\tLast Name\n\n");

      printf("\n\nEnter Data to Display: ");
      scanf("%s",&data);
      while( fscanf(fp1,"%s\t%s\t%s\n",&sn,&fn,&ln) > 0)
      {
            printf("\n%s\t\t\t%s\t\t\t%s",sn,fn,ln);
            system("cls");
            index = 0;
           if((strcmpi(data,"1234")==0)||(strcmpi(data,"John")==0)||(strcmpi(data,"Doend")==0))
               filename = "03/1234.txt";
           else if((strcmpi(data,"3214")==0)||(strcmpi(data,"Jane")==0)||(strcmpi(data,"Dredy")==0))
               filename = "03/3214.txt";
           else if((strcmpi(data,"2104")==0)||(strcmpi(data,"Joey")==0)||(strcmpi(data,"Drone")==0))
               filename = "03/2104.txt";

           fp2=fopen(filename,"r");
           while(fgets(line, sizeof(line), fp2) != NULL)
           { 
                 printf("%s",line);
           }
           fclose(fp2);
      }
      fclose(fp1);
      printf("\n\n");
      system("pause");

}

thank you for that suggested improvement for my program...
but my critical question still stands.

my question is i already inputed the '1234', 'John' and 'Doend'...hence the program is long and I may want add a EDITING function to my program.

if((strcmpi(data,"1234")==0)||(strcmpi(data,"John")==0)||(strcmpi(data,"Doend")==0))

My question is, can I change them into a single variable EACH?
example '1234' is variable 'sn', 'John' is variable 'fn' and 'Doend' is variable 'ln'
and I can still compare it and open the correct file for it.
the correct file to open for John is 03/1234.txt

i did something like this:
i entered the name 'John' this is the variable fn(first name)

if((strcmpi(data,sn)==0)||(strcmpi(data,fn)==0)||(strcmpi(data,ln)==0))

but the computer doesnt open the correct file which is supposed to be 03/1234.txt.
can you tell what should be the right syntax? i would really appreciate it.

sorry for my bad english btw..

Thank you in advance to everyone willing to help me with this trouble and also to Daniweb.com \m/

My question is, can I change them into a single variable EACH?

Yes, declare three strings, assign some value to them, then compare if the if statement

Test the program I posted to see if it works for you.

so basically it looks something like this??? correct me if i'm wrong..

    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>

    int main()
    {
          int opt,opt1,a;
          int x, index;
          char fn[15],ln[15],sn[15],data[15],line[225];
          char locate[15];
          FILE *fp1, *fp2;
          char* filename;

          printf("\t\t\t\t*****MENU*****");
          printf("\n\n\n");
          printf("\t\t\t\t***STUDENT PROFILE***\n\n");

          fp1=fopen("2003.txt","r");
          printf("2003 Student Enrollies\n\n");
          printf("Student Number \t\tFirst Name \t\tLast Name\n\n");

          printf("\n\nEnter Data to Display: ");
          scanf("%s",&data);
          while( fscanf(fp1,"%s\t%s\t%s\n",&sn,&fn,&ln) > 0)
          {
                printf("\n%s\t\t\t%s\t\t\t%s",sn,fn,ln);
                system("cls");
                index = 0;
               if((strcmpi(data,sn)==0)||(strcmpi(data,fn)==0)||(strcmpi(data,ln)==0))
                   filename = "03/1234.txt";
               else if((strcmpi(data,sn)==0)||(strcmpi(data,fn)==0)||(strcmpi(data,ln)==0))
                   filename = "03/3214.txt";
               else if((strcmpi(data,sn)==0)||(strcmpi(data,fn)==0)||(strcmpi(data,ln)==0))
                   filename = "03/2104.txt";

               fp2=fopen(filename,"r");
               while(fgets(line, sizeof(line), fp2) != NULL)
               { 
                     printf("%s",line);
               }
               fclose(fp2);
          }
          fclose(fp1);
          printf("\n\n");
          system("pause");

    }

i changed all the numbers to sn and the others as fn(firstname) and ln(lastname)

if i enter either of the information I gave, how will the computer know what file to open? 03/1234?, 03/3214? or 03/2104?

cuz if i want to input: John or Doend or 1234
the computer should open file 03/1234:
the content of the file 03/1234 is below:

name: (TAB) John
Middle: (TAB) Wanth
Last: (TAB) Doend
S.No.: (TAB) 1234
Address: (TAB) Some random address

status: (TAB) Active

and i would display it using fgets correct? you taught me this last time.

*This program displays the profile of the person in a specific year
*The user could select from the displayed profile who to view

incase you want to know what this program if for ^_^

thank you again for helping me on this Ancient Dragon

and i would display it using fgets correct?

Not quite, remove the & before the three variable names. You don't need to put & to get the address of a char array.

lines 30-35: Notice that the three if statements are all identical. In order to get the correct file name there must be three distictively different test conditions.

Also, I think you want && not ||. You want to test for John AND Wanth AND Doend in order to read file named 03/1234. There might be two or more files that duplicate any one or more of those names, so to get the right one all three must be present at the same time. Very similar to the way you would find someone in a telephone book.

lines 30-35: Notice that the three if statements are all identical. In order to get the correct file name there must be three distictively different test conditions.

Your right..never thought of that...so i revised my program. Instead of comparing three chars, now i use only one variable and that is sn(student number) it seems that my program is to long. If you don't mind a made another program similar to this program only shorter so that maybe its alot easier to understand. I also applied all your suggestion to this revised program.

data.txt content is:

1234 [tab] John [tab] Doend
3214 [tab] Jane [tab] Dredy
2104 [tab] Joey [tab] Drone

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

main()
{
      FILE *fp1,*fp2;
      char sn[5],fn[5],ln[5];
      char srch[5];
      char* filename;
      int y=0;
      fp1=fopen("data.txt","r");
      while(!feof(fp1))
      {
                       fscanf(fp1,"%s\t%s\t%s",sn,fn,ln);
                       printf("%s\t%s\t%s\n",sn,fn,ln);
      }
      fclose(fp1);

      printf("\n\n");printf("Enter Stundent No.: ");
      scanf("%s",&srch);

      fp1=fopen("data.txt","r");
      while(!feof(fp1))
      {
                       fscanf(fp1,"%s\t%s\t%s",sn,fn,ln);

                       if(strcmpi(srch,sn)==0)
                       filename=sn.txt;
                       y=1;

                       fp2=fopen(filename,"r");
                       while(fgets(line, sizeof(line), fp2) != NULL)
                       { 
                                    printf("%s",line);
                       }
                  fclose(fp2);
      }
      fclose(fp1);
      if(y==1)
      printf("Data Found!!");
      else printf("Data Not Found!!");
      getch();
}

here is where all goes wrong...

filename=sn.txt;

didn't know how to solve it lolz...=)
I imagine it to be like this sn.txt = 1234.txt
so that the computer will actually open the correct file.
I tried filename=sn; but sn is only 1234 no extension of .txt

I think we are close in solving this task?
I can really feel it. Have patience with me.

Line 6, should be int main() rather than main().

Instead of filename = sn.txt, You can do this :

strcat( sn, ".txt");

It will concatenate you searched name with ".txt". So now instead of filename, just use sn to open fp2. Hope it works.

ahh finally...but one more thing...i'm trying to put the files inside a folder to make it easier for me to locate them. Example the file 1234.txt is inside the folder named 03, normally you open that like this fopen("03/1234.txt","r") correct?

now using your correct method using strcat(sn,".txt") you add the .txt after the sn. Now how am I going to add the 03/ before the variable sn?

char sn[5] should be char sn[9] (4 more characters to fit '.txt' suffix)

but yes fopen works, remember strings must be wrapped in double quotes "" like

fopen("03/1234.txt", "r")

you need another char array and after strcat( sn, ".txt") you would do strcat( filenameWithFolder, sn)

you need another char array and after strcat( sn, ".txt") you would do strcat( filenameWithFolder, sn)

like this:

strcat(sn,".txt");
strcat("03/",sn);

look at the revised I made:
line 30
line 31

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

main()
{
      FILE *fp1,*fp2;
      char sn[5],fn[5],ln[5];
      char srch[5];
      char* filename;
      int y=0;
      fp1=fopen("data.txt","r");
      while(!feof(fp1))
      {
                       fscanf(fp1,"%s\t%s\t%s",sn,fn,ln);
                       printf("%s\t%s\t%s\n",sn,fn,ln);
      }
      fclose(fp1);

      printf("\n\n");printf("Enter Stundent No.: ");
      scanf("%s",&srch);

      fp1=fopen("data.txt","r");
      while(!feof(fp1))
      {
                       fscanf(fp1,"%s\t%s\t%s",sn,fn,ln);

                       if(strcmpi(srch,sn)==0)
                       strcat(sn,".txt");
                       strcat("03/",sn);
                       y=1;

                       fp2=fopen(filename,"r");
                       while(fgets(line, sizeof(line), fp2) != NULL)
                       { 
                                    printf("%s",line);
                       }
                  fclose(fp2);
      }
      fclose(fp1);
      if(y==1)
      printf("Data Found!!");
      else printf("Data Not Found!!");
      getch();
}

Umm..cuz when I run it and I input the student numbers it sez "*********.exe has stopped working".

There are several issues that need to be addressed.

  1. You declared char* filename which is a pointer to char but you cannot use it until you've allocated memory for it using malloc() or have it point to another pointer or array. By itself it does nothing.

ie.
char* filename = (char*)malloc(number_of_bytes);

So if you wanted a string that is 50 characters in length you would do this:

char* filename = (char*)malloc(50);
strcpy(filename, "this is a string);    // copy a string into filename

Note: malloc() returns a void* pointer so you have to cast it to a char* with (char*)malloc(5)

When you use a pointer, you are given the flexibility to have an array of any size you need. For example, if you wanted the user to enter x amount of grades and then enter his grades x times. You can declare something like float grades[100], but what if the user has 120 grades to enter? or 200? maybe 1000? With pointers you can declare any amount of grades with malloc() based on user input. You can always have a really large array like float grades[100000]; but that's just a waste of space.

But in your case, when working with filenames and paths, you can just declare a static array (fixed size) with a large number like char filename[1024]; or 2056 which I believe is maximum size of a pathname on windows.

  1. When you use strcat() you have to make sure the destination has enough space to add a string to it or it will cause a overflow error and your program will cause unexpected results, most likely crash. So sn[5] holds 5 characters, if sn = "1234" and you add ".txt" then sn becomes "1234.txt" which is 8 characters long causing an overflow.

So you can do something like this

char filename[1024];
strcpy(filename, "03/");    // copy folder (or path) into filename first
strcat(filename, sn);       // add the filename to it
strcat(fielname, ".txt");   // add the file extension
// filename = "03/1234.txt"

This does not work: strcat("03/", sn); because any standalone string you use is a constant string that cannot be modified, and anyway the result is stored in destination (the first parameter) and 'sn' remains unchanged. Just do what I did above.

Your arrays are not big enough. They should at least have a size of 10.

So you can do something like this

Or just use sprintf()

char filename[40];
sprintf(filename,"03/%s.txt", sn);
// filename = "03/1234.txt"
commented: haha yes of course +0

the recent program i posted i forgot to replace one part of the program and i tried to edit but for some reason i cant seem to find the Edit button. But i could use edit button in my last last post.

There are several issues that need to be addressed.

thank you for that great information.
And also to nathan Oliver.

Or just use sprintf()

thank for this too ancient.
after a long revised to my program and i followed almost all your suggestions
here is the program i partially made.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
      FILE *fp1,*fp2,*sy;
      char sn[15],fn[5],ln[5];
      char yr[10],input_yr[10];
      char srch[5],line[225];
      char flnme[1024];
      int y=0, index;

      sy=fopen("year.txt","r");
      while(fscanf(sy,"%s",yr)>0)
      {
                      printf("%s\n",yr);
                      index=0;
      }
      fclose(sy);

      printf("Enter year: ");
      scanf("%s",&input_yr);
      printf("\n\n");

      sy=fopen("year.txt","r");
      while(fscanf(sy,"%s",yr)>0)
      {
                          if(strcmpi(input_yr,yr)==0)
                          {
                                                     strcat(yr,".txt");
                                                     fp1=fopen(yr,"r");
                                                     while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
                                                     {
                                                                                printf("%s\t%s\t%s\n",sn,fn,ln);
                                                     }

                          printf("\n\n");p("Enter Student no.: ");
                          scanf("%s",&srch);
                          system("cls");

                          while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
                          {
                                                              if(strcmpi(srch,sn)==0)
                                                              {
                                                                                     sprintf(flnme,"%s/%s.txt",yr,sn);

                                                                                     fp2=fopen(flnme,"r");
                                                                                     while(fgets(line,sizeof(line),fp2) !=NULL)
                                                                                     {
                                                                                                                        printf("%s",line);
                                                                                     }
                                                                                     fclose(fp2);
                                                              }  
                          }
                          }
                          fclose(fp1);
      }
      fclose(sy);

      getch();
}

year.txt content:

2001
2002
2003
2004

2001.txt content

1234 [tab] John [tab] Doend
3214 [tab] Jane [tab] Dredy
2104 [tab] Joey [tab] Drone

2002.txt content

020121 Fluffy Bunny
021558 Honey Coffee
022482 Fury Rage

1234.txt content

I'm john Doend and I want to
learn Computer programming.

First Question When I run the program it will ask for "Enter year" if i enter 2001 it will display the 2001.txt content then it will ask for Enter Student no.: when i input e.g 1234 then nothing...the question is the loop wrong?

Second Question is if I enter 2002 this will happen "xxxxx.exe" has stopped working,
seriously it worked when i input 2001 and now 2002 nothing?
the file name is 2002.txt i was like staring it for 10-15 seconds just to make sure the file name is correct. @_@

i'm a newbie in programming just to let you know.
also sorry for my bad english.

The code was hard to read with the ever-so-large tabs on every indent, but anyway first things first.
On line 24:
scanf("%s",&input_yr);
and line 40:
scanf("%s",&srch);

When you pass a string to scanf() with "%s" you do not use the address-of operator (&). Why? Because when you declare a string like char input_yr[10] 'input_yr' points to the very first character in the string (so the address of the first character). Using the name 'input_yr' means you're referring to the address of the first character.

For example, if you have the following:

char fname[24] = "John";
In memory it could be like this:

  J     o     h     n
1000  1001  1002  1003

The variable fname then contains the address 1000, but fname has it's own address too.
1000 --> address of 'J' the first letter
2900 --> address of fname (or &fname)

And the other problem is with working with streams like your FILE* structures. The reason when you're asked to enter the student number and nothing happens, is because you've reached the end of the file. How did this happen? Well, you opened up 'fp1' and read the contents and displayed them (lines 33-37).

fp1=fopen(yr,"r");
while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
{
    printf("%s\t%s\t%s\n",sn,fn,ln);
}

Then after you're trying to read data from 'fp1' again (line 43).

while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
{
    [your code here]
}

The issue is once you've reached the end of the file, it does not reset itself. You have to do it with rewind(file_name_here) function which will set the position indicator back to the beginning allowing successive readings to work.

So if you know you've read from the file and you want to read the contents again, call rewind() as such:

rewind(fp1);    // reset position indicator
while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
{
    [your code here]
}

So that means near the beginning of your code, when you open the same file twice, you can just rewind() (check lines 15-28).

sy=fopen("year.txt","r");
while(fscanf(sy,"%s",yr)>0)
{
    printf("%s\n",yr);
    index=0;
}
//fclose(sy);    // no need to close the file if you're going to read from it right after

printf("Enter year: ");
scanf("%s",&input_yr);
printf("\n\n");

// remove the following line (commented out)
//sy=fopen("year.txt","r");
rewind(sy);    // rewind file back to beginning

while(fscanf(sy,"%s",yr)>0)
{
    [code here]
}

Just for clarification, if you have a file called names.txt with 3 names

names.txt
john
gary
russell

And then read the file into a FILE object like FILE* fp, you can imagine the file stream like this:
\n = newline, EOF = end of file

            1            2
01234 56789 01234567 8  90123456789
john\ngary\nrussell\nEOF

Let's say you have variable i = 0 that is the position indicator and char name[64].

After you read the first line, with fscanf(fp, "%s", name), fscanf reads up to the newline (does not extract it) and stores "john" in name, so 'i' moves to the newline located at index 4. Then if you read again, the newline is ignored and "gary" is read (ignoring the newline at index 9), so 'i' becomes index 9.

If you call rewind(fp), the position indicator 'i' resets back to 0 and you can start reading from the beginning of the file again.

Ooops, I probably wrote a lot of stuff, hope it's not too much to read.

Ooops, I probably wrote a lot of stuff, hope it's not too much to read.

not at all, additional informations are always welcome.
so i fixed it heres the program.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
      FILE *fp1,*fp2,*sy;
      char sn[15],fn[5],ln[5];
      char yr[10],input_yr[10];
      char srch[5],line[225];
      char flnme[1024];
      int y=0, index;

      sy=fopen("year.txt","r");
      while(fscanf(sy,"%s",yr)>0)
      {
                      printf("%s\n",yr);
                      index=0;
      }

      printf("Enter year: ");
      scanf("%s",input_yr);
      printf("\n\n");

      rewind(sy);
      while(fscanf(sy,"%s",yr)>0)
      {
                          if(strcmpi(input_yr,yr)==0)
                          {
                          strcat(yr,".txt");
                          fp1=fopen(yr,"r");
                          while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
                          {
                          printf("%s\t%s\t%s\n",sn,fn,ln);
                          }

                          printf("\n\n");printf("Enter Student no.: ");
                          scanf("%s",srch);
                          system("cls");

                          rewind(fp1);
                          while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
                          {

                          if(strcmpi(srch,sn)==0)
                          {

                          sprintf(flnme,"%s/%s.txt",yr,sn);

                          fp2=fopen(flnme,"r");
                          while(fgets(line,sizeof(line),fp2) !=NULL)
                          {
                                                                                                                                   printf("%s",line);
                          }
                          fclose(fp2);
                          }  
                          }
                          }
                          fclose(fp1);
      }
      fclose(sy);

      getch();
}

but the second question still stands if I enter 2002 this will happen "xxxxx.exe" has stopped working.

btw is this correct the file 1234.txt is located inside the folder 2001
so the code i made is this:
sprintf(flnme,"%s/%s.txt",yr,sn);

basically it should suppose to look like this 2001/1234.txt
the variable yr depends on the user inputed. They are all connected. Thats why this code is complicated for me.

When you enter 2002, on line 33 might the problem when reading from 2002.txt

while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)

The format "%s\t%s\t%s" means you want a string [tab] string [tab] string. So your 2002.txt should follow the same format by adding tabs in between, because fscanf() expects that exact format.

2002.txt
020121 [tab] Fluffy [tab] Bunny
021558 [tab] Honey  [tab] Coffee
022482 [tab] Fury   [tab] Rage

So I believe what actually happens is when you try reading the first line from 2002.txt

020121 Fluffy Bunny

fscanf() extracts "020121" and stores it in sn and looks for a \t (tab) but it doesn't find it and it stops reading and returns a value of 1 (indicating number of matching parameters read). So that means variables fn and ln still contain garbage (you didn't read any data into them or initialize them properly), and then you try to print the values with printf("%s\t%s\t%s\n",sn,fn,ln); but the program crashes because fn and ln are garbage data.

It would be better to do some error checking after opening a file and when reading from a file. Because your reading 3 pieces of data from each file, fscanf() should return 3 indicating that it was able to find and match 3 pieces of data to the variables.

FILE* sy;
char filename[1024] = "year.txt";
sy=fopen(filename, "r");    // fopen returns NULL pointer if file cannot be opened
if (sy) {
    do_something();
}
else {
    fprintf(stderr, "Error opening file %s\n", filename);   // print to stream stderr for errors only
}

And your fscanf() should return 'x' amount of data you're expecting.

while (fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)==3) {
    [your code]
}

Or you can do this

int result;

while ((result = fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)) == 3) {     // careful of extra brackets
    [your code here]
}

if (result != 3)
    fprintf(stderr, "Failed to match 3 arguments with file %s\n", filename);
else if (result == EOF)
    fprintf(stderr, "Failed to read data from file %s\n", filename);

Lastly, your code sprintf(flnme,"%s/%s.txt",yr,sn); works fine. The problem is on line 31 you added ".txt" to yr strcat(yr,".txt"); and then you store the student number in sn.

If you entered 2001 for the year and 1234 for the student number then calling sprintf(flnme,"%s/%s.txt",yr,sn); becomes "2001.txt/1234.txt" because you added the ".txt" extension to the year a couple lines back (line 31).

So just declare another variable to store the year and another for the name of the file. For example

char year[24];
char txtfilename[24];
FILE* fp;
// get the year
scanf("%s", year);
// add .txt extension
sprintf(txtfilename, "%s.txt", year);
// open the file "[year_here].txt"
fopen(txtfilename, "r");

When you enter 2002, on line 33 might the problem when reading from 2002.txt

thank you for that dx9...and to all of you guys who helped me out here....

i finally finished my code. =)
once again thank you guys!!

until next task. ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.