Sign in to follow this  
wolwil

How do I fill a char vData[65536] = with a Text File

Recommended Posts

I am trying to get data from a text file into a variable: char vData[65536] = Data from text file I am using C++ 2008 Express I am new to C++ and know very little about it so explanation is key here. main int(void){ char vData[65536];//It has to be 65536!!! while (!EOF){ //Code needed to read a Text.txt line1 and place into vData //Code needed to append the text "\r\n" to vData } cout << vData } **************** Sample Text File **************** abc ABC def DEF hij HIJ **************** What I am hoping to get in cout: abc ABC\r\ndef DEF\r\nhij HIJ\r\n Thanks [Edited by - wolwil on November 9, 2008 9:30:26 PM]

Share this post


Link to post
Share on other sites
Would that be the same as this:

char vData[65536]= "abc ABC\r\ndef DEF\r\nhij HIJ\r\n"
std::cout << vData;
********************************
looks like this When put to cout
********************************
abc ABC
def DEF
ghi GHI

I am trying to do the above example but more or less like this

char vData[65536] = vTextFileData; //where vTextFileData is the lines read from the text file.
std::cout << vData;

)

Share this post


Link to post
Share on other sites
This look like a first year school homework. The answer to the question was answered (use ifstream) and you still ask for help. Ask your teacher instead. Btw, lines COULD be longer than 65536 characters, can't they? The buffer size can vary depending on your use. Also, never use the = operator with strings of characters. You should use strcpy instead or other functions.

Share this post


Link to post
Share on other sites
I can assure you this is not homework. The problem That I have using ifstream is it fills in all the empty space with garbage. So when its passed to the DLL to send to thru the serial port it is sending a ton. I need it to work just as if I typed this in my code:

char vData[65536]= "abc ABC\r\ndef DEF\r\nhij HIJ\r\n"

When I type it like the above it works. I need to replace the words with a variable and thats where I am banging my head against the keyboard. I'm sorry this is not a .Net question but maybe someone can help me anyway. People on here seem to know what they are doing and alot of my code search leads me to this forum so thats why I posted it here.

Share this post


Link to post
Share on other sites
1) Why does your buffer have to be that particular size?
2) Have you verified the contents of your file with a hex editor?
3) Let's take a look at your hard-coded statement.


char vData[65536]= "abc ABC\r\ndef DEF\r\nhij HIJ\r\n"


The string is 27 characters long, right? three lowercase letters, a space, three uppercase letters, and two end of line characters makes 9 per line, times 3 lines.

Now, how many characters are explicitly assigned within vData by that statement?

Answer: 28. A null terminator is added to the end. istream.read() does not do this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this