944,035 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6037
  • C++ RSS
Sep 15th, 2004
0

Using printf with a file

Expand Post »
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.
Last edited by Kiwiman; Sep 15th, 2004 at 3:56 am. Reason: Added to programme
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kiwiman is offline Offline
2 posts
since Sep 2004
Sep 15th, 2004
0

Re: Using printf with a file

Quote originally posted by Kiwiman ...
Correct headers will be called.....
If you only need one, why not just show it?
Quote originally posted by Kiwiman ...
I can't use any other functions than those above.
It is unfortunate that you would be required to use gets.

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);
      }
 
 }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 21st, 2004
0

Re: Using printf with a file

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 */
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kiwiman is offline Offline
2 posts
since Sep 2004
Sep 21st, 2004
0

Re: Using printf with a file

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. const char filename[] = "file.txt";
  6. FILE *file = fopen(filename, "r");
  7. if(file)
  8. {
  9. char line [ BUFSIZ ];
  10. while(fgets(line, sizeof line, file))
  11. {
  12. fputs(line, stdout);
  13. }
  14. fclose(file);
  15. }
  16. return 0;
  17. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: i need ur help
Next Thread in C++ Forum Timeline: Create Function that prints letters, words and sentences





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC