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.....

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);
}


}

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

#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;
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.