11 Replies - 2273 Views - Last Post: 08 October 2012 - 12:39 AM Rate Topic: -----

#1 koji52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-September 12

Searching Using File Handling question

Posted 04 October 2012 - 01:30 AM

hi guys i have a code here using File Handling.
content year.txt is:
2001
2002
2003
2004

content of 2001.txt is:

0123 Codie Banks
0125 Stuid Buri
0126 Chloe huis
0127 halu Wafu

content 0123.txt:

0123 you have successfully
read what is inside the file.

Note: the file 0123.txt is inside a folder name 2001 which means it looks something like this 2001/0123.txt is my code correct:
sprintf(flnme,"%s/%s.txt",yr,sn);
fp2=fopen(flnme,"r");



when you run it goes something like this:

Enter Year: 2001

Displays the year.txt content

Enter Student No.: 0123

displays the content of 0123.txt
note: it displays the content of the 0123.txt IF its not inside a folder, but i want to put it inside a folder to categorize them.

so far its running good.

but when i run it again and enter 2002,2003 or 2004. xxxxxx.exe has stopped working appears.
can you guys take a look and point out where did i go wrong in my code?



#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");p("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();
}



thank you and more power!!

Is This A Good Question/Topic? 0
  • +

Replies To: Searching Using File Handling question

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,165
  • Joined: 08-January 12

Re: Searching Using File Handling question

Posted 04 October 2012 - 02:27 AM

Ignore me.

And thanks for correcting me, Jack :D

This post has been edited by aresh: 04 October 2012 - 07:45 AM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Searching Using File Handling question

Posted 04 October 2012 - 03:29 AM

Uh, a BACKSLASH (\) is used to start an escape sequence, not a forward slash (/).

sy=fopen("year.txt","r");
while(fscanf(sy,"%s",yr)>0)

You never check that you successfully opened the file(s) before using the file pointer(s).
Was This Post Helpful? 2
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Searching Using File Handling question

Posted 04 October 2012 - 03:48 AM

1. Your indentation is poor.
Try this
#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");
      p("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();
}



2. Pick some more meaningful variable names.
char sn[15], fn[5], ln[5];

3. Avoid cute macro substitutions like p for printf
> p("Enter Student no.: ");
http://c-faq.com/cpp/slm.html
Code is mostly read-only. You'll spend a lot more time reading it than writing it, so maximising readability is a must.
If you can't bear the typing, get an editor with auto-completion facilities.

4. Watch your string lengths.
0123 Codie Banks
"Codie" needs at least 6 chars, and you only allocate 5.
Buffer overruns are almost certainly the cause of your crashes.
Was This Post Helpful? 3
  • +
  • -

#5 koji52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-September 12

Re: Searching Using File Handling question

Posted 05 October 2012 - 04:45 AM

thnx for that aresh, JackOfAllTrades and Salem_c...

so now i know how to open a file inside a folder using File handling...
using file handling or any function,how about creating a folder and inside is a file at the same time?

can that be done?? :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#6 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,165
  • Joined: 08-January 12

Re: Searching Using File Handling question

Posted 05 October 2012 - 05:14 AM

Well, it can be done using system() and mkdir, but I'm sure people will know about other methods also.
Was This Post Helpful? 0
  • +
  • -

#7 koji52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-September 12

Re: Searching Using File Handling question

Posted 05 October 2012 - 05:45 AM

can you show some sample codes...no iostream please.
:bigsmile: :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Searching Using File Handling question

Posted 05 October 2012 - 06:19 AM

There isn't a platform independent way to create directories. If you know your target system, then you can make system() calls, or include platform specific headers and make platform API calls to create your directories.
Was This Post Helpful? 0
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,165
  • Joined: 08-January 12

Re: Searching Using File Handling question

Posted 05 October 2012 - 06:42 AM

Well, I think mkdir works on Windows, as well as on Linux. So, you can basically do something like
system("mkdir FOLDER_NAME");

Was This Post Helpful? 0
  • +
  • -

#10 koji52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-September 12

Re: Searching Using File Handling question

Posted 05 October 2012 - 05:02 PM

View Postaresh, on 05 October 2012 - 06:42 AM, said:

Well, I think mkdir works on Windows, as well as on Linux. So, you can basically do something like
system("mkdir FOLDER_NAME");


where will be the created folder located?? if i want to create a folder using that system(mkdir) inside my desired folder.
:red_indian: :red_indian:
Was This Post Helpful? 0
  • +
  • -

#11 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,165
  • Joined: 08-January 12

Re: Searching Using File Handling question

Posted 06 October 2012 - 12:15 AM

I think it will create it inside the active directory. Either way, it is better to specify the full path of where you want your folder to be created.
Was This Post Helpful? 1
  • +
  • -

#12 koji52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-September 12

Re: Searching Using File Handling question

Posted 08 October 2012 - 12:39 AM

thank you for that info
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1