Jump to content
  • Advertisement

Archived

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

Drakon

Easy stuff that I dont know

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

#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 this post


Link to post
Share on other sites
Ouch. Normally I wouldn't entirely correct posted code, but I just have to in this situation.
    
#include <stdio.h>

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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!