Solved

file and directory handling, generating filenames dynamically

Posted on 2006-10-19
20
229 Views
Last Modified: 2010-04-15
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");
}
0
Comment
Question by:Sreekanti
  • 11
  • 9
20 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Which platform?
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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\\";
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
fpath = "c:\\raimi\\escambia\\";

\r is escape sequence for carraige return

0
 

Author Comment

by:Sreekanti
Comment Utility
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?
0
 
LVL 45

Expert Comment

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

2. Print the value in filepath
0
 

Author Comment

by:Sreekanti
Comment Utility
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.
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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.
0
 

Author Comment

by:Sreekanti
Comment Utility
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 :-(

0
 
LVL 45

Expert Comment

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

Author Comment

by:Sreekanti
Comment Utility
I tried:

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

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

I am getting Error 0. What would this mean?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
You had included stdlib.h ?

Post the whole code
0
 

Author Comment

by:Sreekanti
Comment Utility
yep I did include stdlib.h
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Post the whole code ... Copy paste it here
0
 

Author Comment

by:Sreekanti
Comment Utility
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("");
}
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
0
 

Author Comment

by:Sreekanti
Comment Utility
I corrected the data type formatting errors. I am now not getting any errors or warnings. Still the program does not work.
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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??
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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");
 }
0
 

Author Comment

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

Author Comment

by:Sreekanti
Comment Utility
Got it. Thanks again!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now