We help IT Professionals succeed at work.

file and directory handling, generating filenames dynamically

Sreekanti
Sreekanti used Ask the Experts™
on
Hi,

I am writing a C program where I have a directory path from which I need to read several files, one at a time and process them. the file names arise in a pattern. Say RIS01.met, RIS02.met

Now I want to generate a string that stores the path of the file and use it in fopen to open the file. I have pasted here the code that I have written. The problem is that the path that I am specifying in fpath is not working and giving errors on compiling because of the backslash used in the path.

Can you help me find a solution to this problem? Is there an alternative way of generating these file names?

thanks,
Sreekanti

The program

void main()
{
 char *newfile;
 char *ftype;
 char *fname;
 char *fpath;
 int *counter;
 char filepath[150];
 char newfilepath[150];
 FILE *fopen(), *fp, *fp1

 printf("\nWhich set of files do you want to process?\n\n");
  printf("1. ROA\n");
  printf("2. UIS\n");
  printf("3. UCO\n");
  printf("4. ULC\n");
  printf("5. UMA\n");
  printf("6. UOA\n");
  scanf("%d", &ftype);

  counter = 1;

  switch(ftype)
  {
   case 1:
   fname = "ROA0";
   break;

   case 2:
   fname = "UIS0";
   break;

   case 3:
   fname = "UCO0";
   break;

   case 4:
   fname = "ULC0";
   break;

   case 5:
   fname = "UMA0";
   break;

   case 6:
   fname = "UOA0";
   break;
  }
  fpath = "c:\raimi\escambia\";
 }

 sprintf(filepath, "%s%s%d.met\n", fpath, fname, counter);
 fp = fopen(filepath, "r");
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

Commented:
Which platform?
Top Expert 2006

Commented:
from the path it seems it is windows ... Your process of generating path is correct .. Only problem seems to be windows file name handling requires two \\ in path instead of one

Try
fpath = "c:\raimi\\escambia\\";
Top Expert 2006
Commented:
fpath = "c:\\raimi\\escambia\\";

\r is escape sequence for carraige return

Author

Commented:
Thanks for your response. Yes I am working on Windows. Your solution of applying two slashes works and I am not getting any compile errors. However, fopen is not able to identify the file and open it.

While  the sprintf statement is generating the correct path in the variable filepath, fopen is not being able to open the file.

sprintf(filepath, "%s%s%d.met\n", fpath, fname, counter);

fp = fopen(filepath, "r");

When I put another file pointer to open the file by specifying the complete path in quotes, it worked.
fp = fopen("C:\\raimi\\escambia\\RIS01.met", "r");
Any idea, why this problem?
Top Expert 2006

Commented:
>However, fopen is not able to identify the file and open it.
1. What is the error?

2. Print the value in filepath

Author

Commented:
It does not give any particular error. This is how I am testing whether the code is working or not.

  fpath = "C:\\Raimi\\Escambia\\";
  newfile = "C:\\Raimi\\Escambia\\Modfied\\";
 }

 sprintf(filepath, "%s%s%d.met\n", fpath, fname, counter);
 
 printf("%s\n", filepath);
 
 fp = fopen(filepath, "r");
 if (fp == NULL)
 {
  printf("file not found\n");
 }
 else
 {
  printf("file opened successfully\n");
 }
 fp1 = fopen("C:\\raimi\\escambia\\ROA02.met", "r");
 if(fp1 != NULL)
 {
  printf("file opened successfully\n");
 }

When I print the filepath variable it gives me the correct path as a string. In the immediate statement when the variable is entered in fopen, it does not work! File pointer fp is returned as NULL.
Top Expert 2006

Commented:
You are opening in read only mode ... If the file does not exist, call will fail ... Open in rw mode if you are creating the file.

Author

Commented:
Sorry sunnycoder,
 
1. The file does exist in the specified path
2. I nevertheless tried opening the file in rw mode.

It still does not help :-(

Top Expert 2006

Commented:
Check the value in errno .. Use strerror to print the error which is causing fopen to fail
char *strerror( int errnum );

Author

Commented:
I tried:

 sprintf(msg, "%s\n", strerror(errno));

 printf("%s\n", msg);

I am getting Error 0. What would this mean?
Top Expert 2006

Commented:
You had included stdlib.h ?

Post the whole code

Author

Commented:
yep I did include stdlib.h
Top Expert 2006

Commented:
Post the whole code ... Copy paste it here

Author

Commented:
OK here is the code. You may want to create the files that I want to open in the specified directory.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

void main()
{
 int num;
 char *msg;
 char *ch;
 char *ftype;
 char *fname;
 char *fpath;

 int *counter;
 char filepath[100];
 FILE *fopen(), *fp, *fp1;

 system("CLS");
 printf("\n************AUTOMET***************\n");
 printf("\nWhich county met files do you want to process? Escambia(E) or Santa Rosa(S)?\n");
 scanf("%s", &ch);

 if(ch == 'E')
 {
  printf("\n******************Processing met files for Escambia county******************\n");

  printf("\nWhich set of files do you want to process?\n\n");
  printf("1. ROA\n");
  printf("2. UIS\n");
  printf("3. UCO\n");
  printf("4. ULC\n");
  printf("5. UMA\n");
  printf("6. UOA\n");
  scanf("%d", &ftype);

  counter = 1;

  switch(ftype)
  {
   case 1:
   fname = "ROA0";
   break;

   case 2:
   fname = "UIS0";
   break;

   case 3:
   fname = "UCO0";
   break;

   case 4:
   fname = "ULC0";
   break;

   case 5:
   fname = "UMA0";
   break;

   case 6:
   fname = "UOA0";
   break;
  }
  fpath = "C:\\raimi\\escambia\\";
 }

/*Need to write similar else code if santa rosa is chosen*/

 sprintf(filepath, "%s%s%d.met\n", fpath, fname, counter);
 
 printf("%s\n", &filepath);

 sprintf(msg, "%s\n", strerror(errno));

 printf("%s\n", msg);

 fp = fopen(filepath, "r");
 if (fp == NULL)
 {
  printf("file not found\n");
 }
 else
 {
  printf("file opened successfully\n");
 }
 fp1 = fopen("C:\\raimi\\escambia\\ROA02.met", "r");
 if(fp1 != NULL)
 {
  printf("file opened successfully\n");
 }
 getch("");
}
Top Expert 2006

Commented:
There are several errors in your program ... It is a good idea not to ignore compiler warnings ... e.g.
>scanf("%s", &ch);
ch is a char pointer and you are reading in a string at its address .. later in the code, you treat ch as a char !!!
Do not mix the types and format strings ... Results will be unpredictable

Author

Commented:
I corrected the data type formatting errors. I am now not getting any errors or warnings. Still the program does not work.
Top Expert 2006

Commented:
Took me 1 hour of agony to locate this bug

sprintf(filepath, "%s%s%d.met\n", fpath, fname, counter);
                                            ^^ Why do you have this newline in this string??
Top Expert 2006

Commented:
Btw .. a side note

sprintf(msg, "%s\n", strerror(errno));    >>>>>> strerror should be called after the erring call (here fopen) .. before that call, all wil be fine and dandy. ... Move strerror call to if ( fp == NULL) block

 printf("%s\n", msg);

 fp = fopen(filepath, "r");
 if (fp == NULL)
 {
  printf("file not found\n");
 }

Author

Commented:
Thanks a million! I don't why or how I put it that newline character there! My silly mistake. It works perfect now. Thanks!

Author

Commented:
Got it. Thanks again!