Skip to main content
gamedev.net
🔒 Locked

Loading .txt Files

Started by Aggrix Jan 6, 2002 at 9:08 PM 9 replies 500+ views
Original Post
Aggrix
Aggrix
I am having trouble loading a .txt file into my Win 32 program. I think it has something to do with the memory. I get the path name to the file using the GetOpenFileName function and use this code to load it: FILE* File; long Size; char* text; File = fopen(FileName, "r"); fseek(File, 0, SEEK_END); Size = ftell(File); rewind(File); text = (char*)malloc(Size); fread(text, 1, Size, File); SetWindowText(hEdit, text); fclose(File); free(text); It comes up in the Edit Control but it reads testýýýý1 any idea how to solve this? Thanx
Aggrix
Aggrix
Thanks a lot, I was trying to figure that out for about 3 days.
capn_midnight
capn_midnight
dude, get away from C, get on the C++ bandwagon.
I thought for awhile that I should learn C before C++, but then I got really pissed at the terrible pointers deal with C and switched.
Code to read in from a file in C++ is almost identical to code to read in from the keyboard:

#include <iostream.h>
#include <fstream.h>

int main(void)
{
ifstream fin("data.txt");
int x;
cout<<"read in from keyboard"<<endl;
cin>>x;
cout<<"read in from file"<<endl;
fin>>x;
fin.close();

return 0;
}


see how easy!!! No dealing with pointers!!! No learning new functions!!! It's so easy!!! This is why I switched right here!!!

Edited by - capn_midnight on January 7, 2002 12:56:06 PM
Mullen
Mullen
But you''ll have to realize that in game programming you''ll be using pointers fairly often.

But I agree. C++ is nicer.
" When you know what is; you''ll know what isn''t. "~Miyamoto Musashi
Ferinorius
Ferinorius
ok then, new question....

using c++ is nicer, but is it reliable enough for game save files that load in a lot of numbers, strings and other things?

just wondering, i like the c++ way, but every example or tut i see shows c examples.



Neo-Toshi - The City That Never Sleeps
ShawnO
ShawnO
quote:
Original post by Anonymous Poster
Same thing in c (notice, no pointers either):

    
#include <stdio.h>
#include <conio.h>

int main(void)
{
FILE *in = fopen("data.txt","r");
int x;
printf("read in from keyboard\n");
x=getch();
printf("read in from file\n");
x = getc(in);
fclose(in);
}


Sorry to be a nit picker, but what do you call FILE *in? It sure looks like a pointer to me.
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
Zeusel
Zeusel
Sorry to be a nit picker, but what do you call FILE *in? It sure looks like a pointer to myou cant count that! this pointer is sooooo simple to use!
"be fair! ... always"Zeusel
Kwizatz
Kwizatz
Well, C is easier to understand for someone new, specially if you are explaining an algorithm.

I think its easier to teach how to write file I/O functions first, and then when that part is understood, teach how to implement a file I/O Class.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.