Originally Posted by
bored_guy
hi,
i am writing a simple program whereby the user is asked to key in the full path of a folder, and then the program is to search for a file with a particular name, open it for processing, and then to create an output file with a fixed name in that same folder. for instance, the program may look for a file named 'image' and output something like 'processed_image'.
are there some straightforward codes to do that? thanks!
There are various file handling functions in C. Usually including something akin to <dir.h> that has these functions.
I haven't used them, so I can't advise you on them. Here's a "roll your own" approach:
Code:
#include <stdio.h>
int main() {
FILE *fp;
char fullFileName[100];
int i;
fgets(fullFileName, sizeof(fullFileName), stdin);
/* e.g.: C:\TC\file_get.txt<enter>
i = 99;
while(i > -1) { //must strip off the newline on the end
if(fullFileName[i] == '\n')
fullFileName[i] = '\0'; //and properly adjust the string
--i;
}
if((fp = fopen(fullFileName, "rt"))==NULL) {
perror("Error opening fullFileName");
return 1;
}
while((fgets(fullFileName, sizeof(fullFileName), fp)) != NULL)
printf("\n\n\n %s", fullFileName);
i = getchar(); ++i;
return 0;
}
You know you're getting off very easy here. Next time, be prepared to show some code, partner.