Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to read in char data from file and recast as int data

Status
Not open for further replies.

ssdave

Programmer
Jul 8, 2002
9
0
0
US
Once, again, I apologize for the inane question, I am very much a beginner. How do you read in string (char?) data, and recast it as int?

FILE *stream ;
FILE *stream2 ;
FILE *stream3 ;


int buffer[1024];
int buffer2[1024];
int buffer3[1024];


do{
m_refresh_3 = fread(buffer, sizeof(buffer),25,stream);
m_fpsize = fread(buffer2, sizeof(buffer2), 25, stream2);
m_minpoints = fread(buffer3, sizeof(buffer3),25,stream3);
}while ( !feof(stream3));

fclose(stream);
fclose(stream2);
fclose(stream3);

The above code does not error out, however, he values in each of my three variables is "-858993460".

Thanks in advance for the help.


 
ok, I've learned so far that -858993460 is a default value that the compiler assigns to a variable when it doesn't know what else to do. Why aren't my variables being populated by my read statement?
 
Hi,
what are

m_refresh_3
m_fpsize
m_minpoints


I don't see them declared anywhere.

From the fread man page.....

fread reads into an array pointed to by ptr up to nitems
items of data from stream, where an item of data is a
sequence of bytes (not necessarily terminated by a null
byte) of length size.

fread returns the number of items read.


Therefore you are asking it to read 25 items of 4096 bytes in size into a buffer of 4096 bytes.

I am surprised your program isn't overwriting the stack and core dumping.


 
thanks for responding,
m_refresh_3
m_fpsize
m_minpoints

are variables declared in the header for this particular implementation file as type "int".

there are a number of ways to read a file, which method should I use? "Fread" is the wrong one, since it's pulling the number of bytes from the file and not the characters themselves? How do I pull the characters out of the file as numbers?
 
Help!!!

"This conversion requires a reinterpret_cast, a C-style cast or function-style cast"

How do I do a "reinterpret_cast"???
 
I'm not sure what you are trying to do

1) What is the format of your data? Are you trying to read binary or ASCII numbers

m_refresh_3 etc will tell you how many bytes were read. These have to be unsigned ints. Is that what you want?

2) fread(buffer, sizeof(buffer), 25, stream) is telling the machine to read 25 buffers, each of size sizeof(buffer). This means you are reading 25K of data into a 1K buffer. Is this really what you want???

3) Do you want to read 25 numbers into an array called m_refresh_3?
 
If you have data in string form,
static_cast<int>(stringName) will convert the string from string to int form.
 
You know what, I lied twice, and don't know how to delete posts here. Sorry.

Use atoi( stringName ).
 
Thanks boulder bum, that worked great!! I changed my read to getline method(?) and used atoi to convert.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top