954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using printf with a file

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.

Kiwiman
Newbie Poster
2 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 
Correct headers will be called.....

If you only need one, why not just show it?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);
      }
 
 }
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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 */

Kiwiman
Newbie Poster
2 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 
#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;
 }
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You