Thread: Problem with giving multiple files as an input to a C program.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    Problem with giving multiple files as an input to a C program.

    Code:
    for(i=1;i<3;i++)
     {
       sprintf(fn,"LXL[%d].pdb",i);
        if((fptr1=fopen(fn, "r"))==NULL)
          {
            printf("Problem, cannot open the file for reading.\n");
           }
       else
             if((fptr2=fopen(filename2, "a"))==NULL)
          {
             printf("Problem, cannot open %s for writing.\n", filename2);
           }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So what's your question?

    A random bit of code with no context and a "it doesn't work" topic doesn't help much.

    char *fn;
    char fn[5];
    for example could be the simple mistake.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    Quote Originally Posted by Salem View Post
    So what's your question?

    A random bit of code with no context and a "it doesn't work" topic doesn't help much.

    char *fn;
    char fn[5];
    for example could be the simple mistake.
    Code:
    void main()
    {
    int i,c;
    char fn[40],ch;
    char filename2[200];
    FILE *fptr1,*fptr2;
    clrscr();
    printf("Enter name of the destination file:");
    scanf("%30s",filename2);
    
    
    for(i=0;i<30;i++)
    {
       sprintf(fn,"C:\\USERS\\LXL[%d].pdb",i);
       printf("%s",fn);
    
    
      if((fptr1=fopen(fn, "r"))==NULL)
           {
    	printf("Problem, cannot open the file for reading.\n");
           }
      else
     
      if((fptr2=fopen(filename2, "a"))==NULL)
           {
    	printf("Problem, cannot open %s for writing.\n", filename2);
           }
      else
           {
    	 LineReadWrite(fptr1, fptr2);
    	 if(fclose(fptr1)==0)
    	 printf("The file is successfully closed.\n");
    
    
    	 if(fclose(fptr2)==0)
    	 printf("%s successfully closed.\n", filename2);
           }
    I have 30 files, which i need to read and extract info and write the data in a single file. I hope its clear now.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void main()
    Mmmm.

    > clrscr();
    Does this mean you're using TurboC?
    TurbidCrap doesn't do long files and filenames with spaces (in case that's another "edit of convenience") which is not your real code.

    > printf("Problem, cannot open the file for reading.\n");
    Also lookup perror() to find out the real reason.

    > I have 30 files, which i need to read and extract info and write the data in a single file
    Apart from not being able to open the file, another plausible reason is "too many open files", because it seems that you manage to open a lot of files, but your code is incapable of closing files.
    Does it fail on the first file, or the 20th?

    A console log would also help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    The code seems Ok to me.

    If fopen() fails, firstly print out the filename you are sending it, as you have done. Then check that the file actually exists, is not read protected or in use by another application.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    Quote Originally Posted by Malcolm McLean View Post
    The code seems Ok to me.

    If fopen() fails, firstly print out the filename you are sending it, as you have done. Then check that the file actually exists, is not read protected or in use by another application.
    [QUOTE]
    Every thing is fine, i am even getting 'file name" as an output but after that it is giving some error. So, file is there, it is successfully opened to read but beyond that it seems there is some problem.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    Quote Originally Posted by Salem View Post
    > void main()
    Mmmm.

    > clrscr();
    Does this mean you're using TurboC?
    TurbidCrap doesn't do long files and filenames with spaces (in case that's another "edit of convenience") which is not your real code.

    > printf("Problem, cannot open the file for reading.\n");
    Also lookup perror() to find out the real reason.

    > I have 30 files, which i need to read and extract info and write the data in a single file
    Apart from not being able to open the file, another plausible reason is "too many open files", because it seems that you manage to open a lot of files, but your code is incapable of closing files.
    Does it fail on the first file, or the 20th?

    A console log would also help.
    Firstly, i dont think VOID main() is giving me any trouble.
    And as far as "30 files" are concerned i am getting the same error when i am running it for just 2 files!!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So when are you going to call perror() to find out why it fails?

    - file not found
    - permission denied
    - too many open files

    WTF is actually happening!?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    giving some error
    Why do people who think they are programmers believe we can see their computer screen?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help my program is giving a problem
    By zangetsu in forum C Programming
    Replies: 72
    Last Post: 04-04-2011, 10:56 AM
  2. Handling multiple input files from command line
    By cnfwriter in forum C Programming
    Replies: 13
    Last Post: 08-25-2008, 08:07 AM
  3. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  4. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM
  5. How to get input from multiple files?
    By jumpyg in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 09:20 PM

Tags for this Thread