Hi,
basically what I have to do is write a filter program that opens a file for reading. This file is User defined during runtime.
Anybody got any ideas on how to do this??
Printable View
Hi,
basically what I have to do is write a filter program that opens a file for reading. This file is User defined during runtime.
Anybody got any ideas on how to do this??
Code:char filename[FILENAME_MAX];
FILE *fp;
printf ( "File to open: " );
fflush ( stdout );
if ( fgets ( filename, sizeof filename, stdin ) != NULL ) {
char *newline = strrchr ( filename, '\n' );
if ( newline != NULL )
*newline = '\0';
fp = fopen ( filename, "r" );
if ( fp == NULL )
perror ( NULL );
...
}
Prelude,
sorry but when I try to use your code the complier gives two errors
[C++ Error] DUMPFILE.cpp(47): E2268 Call to undefined function 'strrchr'.
[C++ Error] DUMPFILE.cpp(47): E2034 Cannot convert 'int' to 'char *'.
for this line of code
char *newline = strrchr ( filename, '\n' );
any ideas??
thanks
just got it to work,
my fault forgot to add in string.h
thanks for taking the time to help me out
Here's an idea, try compiling as C instead of C++. That'll help with other issues later on that you'll end up asking about.Quote:
Originally posted by john_newbie
the complier gives two errors
[C++ Error] DUMPFILE.cpp(47): E2268 Call to undefined function 'strrchr'.
[C++ Error] DUMPFILE.cpp(47): E2034 Cannot convert 'int' to 'char *'.
any ideas??
Quzah.
John.
Code:/*
Simple file open/close program in C.
Error checking omitted for sanity.
*/
#include <stdio.h>
int main(int argc, char **argv)
{
FILE* fptr; /* File object... */
if (argc != 2) /* Display usage. */
{
printf("Usage: %s [filename]\n", argv[0]);
}
else
{
fptr = fopen(argv[1], "r"); /* Open file... */
printf("%s opened for reading.\n", argv[1]);
fclose(fptr); /* Close file... */
printf("%s closed.\n", argv[1]);
}
return 0;
}
>Error checking omitted for sanity.
I can understand your reasoning, but it really is better to assume that the people here are not stupid. If you post bulletproof code then it sets a good example.
Off-topic bickering snipped