I want to use a vector of vector for my data handling in c++. The data str uctre which i want to create is like:
1 234,345, 342, 12
2 12, 321, 210
3 108, 23,234, 23, 12
so i have PointNumber and PointList of each pointnumber. How to create a vector of vector to holding these values. Do i need to cretae class ?
vector<int,vector<int> >My_points(){int PointNumber, vector<int>PointList};
is it correct? Please help me to define this correct way.
Is it possible to add data into vector from a array? ( my PointList is a array )
For converting the ASCII into binary, I'm guessing you mean into binary representation of these floating-point numbers. With that assumtion in mind, here is some pseudo code to think about:
Create a structure of five floating-point numbers such as this:
open input file A.txt for reading (and check for success)
open output file B.dat for writing (and check for success)
while not the eof of the input file:
{
read a line of text
if eof of the input file,
break;
for each number in the line:
{
convert the number to a floating-point number (say with atof() )
assign the floating-point number to one of the fields in the structure (I'll leave this up to you to figure out)
}
write the structure to the output file.
}
close the input file
close the output file
open input file B.dat for reading (and check for success)
open output file C.txt for writing (and check for success)
while not the eof of the input file:
{
read in the structure
if eof of the input file,
break;
for each value in the structure:
{
convert the number to an ASCII string (say with sprintf() )
write the ASCII string to the output file.
}
write a newline to the output file.
}
close the input file
close the output file
Keep in mind that once you start write()ing floating point numbers to file you open yourself up to representation and endianness issues.
You might want to say, for example, that the file contains IEEE 754-1985 Single Precision values in Little-Endian byte order. (This makes it easy to just load and dump the values on a PC, but should require your code to recognize when it is being used on other systems...)
Thank you very much kooth, but still I am slugging since I am very poor in c++ programming. But I tried to write c++ codes according to your procedure and comments. However I could not complete it and have several mistakes in my codes (see below). Especially I don’t know that how to write codes for the convertion part (for each number in theline:
{
convert the number to a floating-point number (say with atof() )
assign the floating-point number to one of the fields in the structure (I'll leave this up to you to figure out)
}
write the structure to the output file. })
could you pls help me to complete Ascii to binary part, then hopefully I can try binary to ascii part? So I needs help to write c++ codes. Many thanks
FiveFloats Five;
int i = 0;
string
AString,
Line;
while( ! InTextFile.eof() }
{
getline( InTextFile, Line );
if( InTextFile.eof() )
break;
/* YOU FIGURE OUT HOW TO PARSE THE LINE :) */
for each field (let's say we assign it to AString):
{
AString = YourParsingFunction( i );
float aFloat = atof( AString.data() );
switch( i )
{
case 0:
Five.a = aFloat;
break;
case 1:
Five.b = AFloat;
break;
( And so forth )
} /* switch( i ) */
} /* for( each field in the line ) */
OutBinFile.write( ( const char * )&Five, sizeof( Five ) );
} /* while( ! InTextFile.eof() ) */