| | |
Using printf with a file
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Solved Threads: 0
Hi everyone, I'm doing a beginners C++ paper at Uni and I'm stuck. Basically I need to read data from a file and then display it on the screen. So far I've got:
Correct headers will be called.....
int main(){
char filename[30];
FILE *in;
printf("Enter file you wish to open\n");
gets(filename);
in = fopen(filename,"r");
printf("%s", 'in');
if (in == NULL) {
printf("Error could not open %s\n", filename);
exit(1);
}
}
I can't use any other functions than those above. Where am I going wrong?? THanks.
Correct headers will be called.....
int main(){
char filename[30];
FILE *in;
printf("Enter file you wish to open\n");
gets(filename);
in = fopen(filename,"r");
printf("%s", 'in');
if (in == NULL) {
printf("Error could not open %s\n", filename);
exit(1);
}
}
I can't use any other functions than those above. Where am I going wrong?? THanks.
Last edited by Kiwiman; Sep 15th, 2004 at 3:56 am. Reason: Added to programme
•
•
•
•
Originally Posted by Kiwiman
Correct headers will be called.....
•
•
•
•
Originally Posted by Kiwiman
I can't use any other functions than those above.
Code is much easier to read when it is within [code][/code]
int main(){
char filename[30];
FILE *in;
printf("Enter file you wish to open\n");
gets(filename); /* eeeeeah */
in = fopen(filename,"r");
printf("%s", 'in'); /* 'in' is not a valid character, not a valid string, nor a valid file */
/* Let's assume that you are trying to read from a file. First, don't write to it. */
/* You should be using fgets to get a string from a file or the stdin */
if (in == NULL) { /* a little late for the check if you're already attempting to mess with it above */
printf("Error could not open %s\n", filename);
exit(1);
}
} "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Solved Threads: 0
With regard to the correct headers being called, this only the beginning of a long winded assignment where the data is to be manipulated aswell. Have to be able to delete a line the user selects or replace a line and then save the file aswell. I've pretty much given up and am putting this paper down to a bad selection. The resources provided to us are worse than useless and as we are unable to use functions beyond what the lecturer has stipulated we are strangled as to asking competent programmers for any real help. Thanks for your attempted help but I shall now throw something hard against a wall, perhaps me.
/* end rant */
/* end rant */
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { const char filename[] = "file.txt"; FILE *file = fopen(filename, "r"); if(file) { char line [ BUFSIZ ]; while(fgets(line, sizeof line, file)) { fputs(line, stdout); } fclose(file); } return 0; }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Bubble sort & File output jibrish errors??? (C)
- file types: eliminating leading white spaces (C)
- File I/O Help (C)
- File writing Error (C)
- reading a file into code (Java)
- file input and output (C)
Other Threads in the C++ Forum
- Previous Thread: i need ur help
- Next Thread: Create Function that prints letters, words and sentences
Views: 4097 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






