Solved

file and directory handling, generating filenames dynamically

Posted on 2006-10-19
20
257 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
ID: 17763805
Which platform?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17763844
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
ID: 17763851
fpath = "c:\\raimi\\escambia\\";

\r is escape sequence for carraige return

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Sreekanti
ID: 17766180
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
ID: 17766221
>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
ID: 17766278
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
ID: 17766305
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
ID: 17766358
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
ID: 17766494
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
ID: 17766665
I tried:

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

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

I am getting Error 0. What would this mean?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17766674
You had included stdlib.h ?

Post the whole code
0
 

Author Comment

by:Sreekanti
ID: 17766773
yep I did include stdlib.h
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17766789
Post the whole code ... Copy paste it here
0
 

Author Comment

by:Sreekanti
ID: 17766929
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
ID: 17767282
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
ID: 17767400
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
ID: 17767674
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
ID: 17767699
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
ID: 17767701
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
ID: 17767712
Got it. Thanks again!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

860 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