Reading an ENTIRE file and feeding it into a buffer/string??

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Reading an ENTIRE file and feeding it into a buffer/string??

Post by sg57 »

Hi

Havent posted here in a while as google has been friendly for me, until now. It has failed me.

I made some credits that work fine, ya da ya da, but I had to add some things here and there, so I made it after scrolling by everyone, read a file called 'credits' and inside there is an 'Image' made out of symbles and such, now I want to display these 'strings'/'characters' and such as they are in the file, so far I have loaded them fine, read them fine, but I cant find a call that will read more than one line of the text, I have only gotten it to flash the entire text, but only 1 char by 1 char, at the position i set it at, does anyone think they can help?

I wil however keep working on it though....
framerate
Posts: 20
Joined: Sun Aug 14, 2005 2:30 am

Post by framerate »

I might be wrong, but instead just doing something (after opening the file) like:

Code: Select all

char* line;
Couldn't you just do a 2-d array.. something like

Code: Select all

char** line;
or

Code: Select all

char* line[0]; // instead of using the dual pointers...
that way you load each line (one by one) into a larger arry, and then can display the lines one by one by looping through the 2-d array?[/code]
DustinFraze
Posts: 13
Joined: Fri Jan 06, 2006 5:44 pm

Re: Reading an ENTIRE file and feeding it into a buffer/stri

Post by DustinFraze »

sg57 wrote:but I cant find a call that will read more than one line of the text, I have only gotten it to flash the entire text, but only 1 char by 1 char, at the position i set it at, does anyone think they can help?
fread()?

Or am I mis-understanding the problem?
framerate
Posts: 20
Joined: Sun Aug 14, 2005 2:30 am

Re: Reading an ENTIRE file and feeding it into a buffer/stri

Post by framerate »

DustinFraze wrote:
sg57 wrote:but I cant find a call that will read more than one line of the text, I have only gotten it to flash the entire text, but only 1 char by 1 char, at the position i set it at, does anyone think they can help?
fread()?

Or am I mis-understanding the problem?
Yeah, I wasn't 100% on what his problem was either...
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

Wow, Im an idiot (not really), I just glanced over fread(), and saw it loaded it all into a buffer, but didnt think I could print the buffer, god (smacks himself), Ill use fread(), ill post back here if I decide to have it print character x character, maybe have it do that while moving the X co-ord 10 over. But how do Istop if from disappearing?
DustinFraze
Posts: 13
Joined: Fri Jan 06, 2006 5:44 pm

Post by DustinFraze »

sg57 wrote:But how do Istop if from disappearing?
?

The text disappears? Then what? Does the application continue to run? Do you get spit to vsh?

If the text disappears, does it then re-appear, like a flashing effect? Or does it pop up only long enough to be seen, then it's gone for good?
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

NVM about that, fread doesnt work....

here is a snippet:

Code: Select all

    FILE *credFile;                             // FILE pointer
    credFile = fopen("data/credits", "r");     // Initialize the pointer to open credits
    long lSize;
    char * Cbuffer;

    while(1) {
									  fseek (credFile , 0 , SEEK_END);
									  lSize = ftell (credFile);
  									rewind (credFile);
  									Cbuffer = (char*) malloc (lSize);
										fread (Cbuffer,1,lSize,credFile);             // Print the text from credit file
    								printTextScreen(5, 10, Cbuffer, RGB(255, 255, 255));
    								fclose(credFile);
    								free (Cbuffer);
}
Well? That should get the size of a file credits, then seek the last character in it, go back to start of credits file, use the malloc to find set the memory needed, then read the file with all the previous actions included/done already, then print the writing to the screen, anything worng? i get an exceptio handler
Dr. Vegetable
Posts: 171
Joined: Mon Nov 14, 2005 1:32 am
Location: Boston, Massachusetts
Contact:

Post by Dr. Vegetable »

You are (most likely) reading the text from the file. But you need to terminate the buffer with a NUL (0x00) byte so that printTextToScreen() will know when to stop. Otherwise it will try to access memory beyond the end of Cbuffer, which will cause an exception.

Try these changes:

Cbuffer = (char*) malloc (lSize+1);
fread (Cbuffer,1,lSize,credFile);
Cbuffer[lSize] = '\0'; // NUL-terminate all C strings!

Hope this helps!
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

Thanks i will. I have tried alot of different reading functions. Hope it works...
Post Reply