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??
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??
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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 ); ... }
My best code is written with the delete key.
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.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.
Hope is the first step on the road to disappointment.
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; }
R.I.P C89
>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.
My best code is written with the delete key.
Off-topic bickering snipped
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.