#### Archived

This topic is now archived and is closed to further replies.

# Easy stuff that I dont know

This topic is 5969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know this is a very simple thing, but... How do make a simple Win32 Console app open and display a text file?

##### Share on other sites

#include stdio.h
#include conio.h

int main()
{
char filename[256];
FILE *fp;
char cTemp;

fscanf("%s", filename); //Assumes no spaces in filename

fp = fopen(filename, "rb");

while(!feof(fp))
{
fread(&cTemp, sizeof(char), 1, fp);
printf("%c", cTemp);
}

fclose(fp);
getch();
return 0;
}

##### Share on other sites
Ouch. Normally I wouldn't entirely correct posted code, but I just have to in this situation.
  #include int main(void) { char Filename[256]; FILE *File; scanf("%s",Filename); /* Also assumes no spaces */ File = fopen(Filename,"rt"); while(!feof(File)) { putchar(fgetc(File)); } fclose(File); getchar(); return 0;}

That code wouldn't just not work efficiently, it would crash. Of course, my code is only a little more efficient (I wanted to keep it simple) and has no error checking. It also sticks to ANSI functions.

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on October 17, 2001 6:07:39 PM