Using printf with a file

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 2
Reputation: Kiwiman is an unknown quantity at this point 
Solved Threads: 0
Kiwiman Kiwiman is offline Offline
Newbie Poster

Using printf with a file

 
0
  #1
Sep 15th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using printf with a file

 
0
  #2
Sep 15th, 2004
Originally Posted by Kiwiman
Correct headers will be called.....
If you only need one, why not just show it?
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);
      }
 
 }
"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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 2
Reputation: Kiwiman is an unknown quantity at this point 
Solved Threads: 0
Kiwiman Kiwiman is offline Offline
Newbie Poster

Re: Using printf with a file

 
0
  #3
Sep 21st, 2004
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 */
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using printf with a file

 
0
  #4
Sep 21st, 2004
  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. }
"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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4097 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC