Jump to content
  • Advertisement
Sign in to follow this  
wolwil

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

This topic is 3508 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 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
Advertisement
You can use the standard library stream support to read the data from the file. ifstream is probably the class you're seeking. For example:


std::ifstream input("text.txt");
input.read(&vData[0], 65536);

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
Why don't you try doing some work for yourself and find out? You won't become a good programmer by asking people on the internet to solve all of your problems for you.

Also, you posted in the wrong forum. This isn't remotely .NET related.

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
Sign in to follow this  

  • 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!