File I/O
-
I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....
-
I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....
The way you have presented it, it looks like the choice is one of personal preference, rather than necessity. How much coding is already done? Do you already have in place the code for reading the file line by line and parsing the data, or not? Rich
-
I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....
What does the dots at the end of each line signify? Steve
-
I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....
-
The way you have presented it, it looks like the choice is one of personal preference, rather than necessity. How much coding is already done? Do you already have in place the code for reading the file line by line and parsing the data, or not? Rich
Currently I've coded a chunk of code that extracts line by line. I've coded in Visual Basic and successfully extracted each of the input using this code. Open FilePath For Input As #1 Delimiter = "," SegmentCount = 1 ReDim TheSegments(1) Do Until EOF(1) DoEvents If UBound(TheSegments) = SegmentCount Then ReDim Preserve TheSegments(SegmentCount + 10) End If ' Read a line from the vin file Line Input #1, TheString ' Parse the line SubStrings = Split(TheString, Delimiter, -1, vbTextCompare) ' Transfer all data to a Segment '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ' Get the name(description) TheSegments(SegmentCount).Name = SubStrings(0) ' Get the Front Plane TheSegments(SegmentCount).FrontPlane = Val(SubStrings(1)) ' Get the Back Plane TheSegments(SegmentCount).BackPlane = Val(SubStrings(2)) ' Get the Color TheSegments(SegmentCount).Color = Val(SubStrings(3)) ' Calculate the number of pixels PixelCount = UBound(SubStrings) - 4 TheSegments(SegmentCount).PixelCount = PixelCount ' Now get all the pixel positions For Count = 1 To PixelCount TempLong = Val(SubStrings(Count + 3)) 'TheSegments(SegmentCount).Pixels(Count).Val = TempLong TheSegments(SegmentCount).Pixels(Count).x = TempLong Mod 320 TheSegments(SegmentCount).Pixels(Count).y = TempLong \ 320 Next '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& SegmentCount = SegmentCount + 1 ''''''''''''''''''''''''''' Loop Close #1 End Sub In C++ i have no idea how to deal with this... I'm currently into self indulgement with this language at the same time working on Hardware and Software design of my project LCD USB Emulator...
-
I'm currently studying string extractions. I can't focus really on software side coz i'm also designing the hardware... Thanks Chris, I'll be trying it....
-
Currently I've coded a chunk of code that extracts line by line. I've coded in Visual Basic and successfully extracted each of the input using this code. Open FilePath For Input As #1 Delimiter = "," SegmentCount = 1 ReDim TheSegments(1) Do Until EOF(1) DoEvents If UBound(TheSegments) = SegmentCount Then ReDim Preserve TheSegments(SegmentCount + 10) End If ' Read a line from the vin file Line Input #1, TheString ' Parse the line SubStrings = Split(TheString, Delimiter, -1, vbTextCompare) ' Transfer all data to a Segment '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ' Get the name(description) TheSegments(SegmentCount).Name = SubStrings(0) ' Get the Front Plane TheSegments(SegmentCount).FrontPlane = Val(SubStrings(1)) ' Get the Back Plane TheSegments(SegmentCount).BackPlane = Val(SubStrings(2)) ' Get the Color TheSegments(SegmentCount).Color = Val(SubStrings(3)) ' Calculate the number of pixels PixelCount = UBound(SubStrings) - 4 TheSegments(SegmentCount).PixelCount = PixelCount ' Now get all the pixel positions For Count = 1 To PixelCount TempLong = Val(SubStrings(Count + 3)) 'TheSegments(SegmentCount).Pixels(Count).Val = TempLong TheSegments(SegmentCount).Pixels(Count).x = TempLong Mod 320 TheSegments(SegmentCount).Pixels(Count).y = TempLong \ 320 Next '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& SegmentCount = SegmentCount + 1 ''''''''''''''''''''''''''' Loop Close #1 End Sub In C++ i have no idea how to deal with this... I'm currently into self indulgement with this language at the same time working on Hardware and Software design of my project LCD USB Emulator...
So you want to pass the data to a C++ routine after it has been extracted by the VB program, or you just want to translate all of the VB code into a C++ program?
-
What does the dots at the end of each line signify? Steve
Steve, Sorry for the dot stuff, that signifies another input of type long int... actually the first entry is Char, then the second, third and the fourth is of type int, the fifth until end of line is a type of long int. each line have different number of items. I'm not good in explaining things. Thanks Steve
-
So you want to pass the data to a C++ routine after it has been extracted by the VB program, or you just want to translate all of the VB code into a C++ program?
I just want to translate the VB code into C++ program. It's a bit difficult on my part. Thanks
-
I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....
char char_val; int int_val1, int_val2, int_val3; long int longint_val1; char comma; ifstream fs("C:\\a.txt"); if ( !(fs >> char_val >> comma) || comma!=',') { // Bail.... } if ( !fs >> int_val1 >> comma) || comma!=',') { // Bail.... } // etc...
Steve
-
char char_val; int int_val1, int_val2, int_val3; long int longint_val1; char comma; ifstream fs("C:\\a.txt"); if ( !(fs >> char_val >> comma) || comma!=',') { // Bail.... } if ( !fs >> int_val1 >> comma) || comma!=',') { // Bail.... } // etc...
Steve
Thanks a lot Steve, I'm currently coding it in my C++ BuilderX compiler. Thanks........
-
char char_val; int int_val1, int_val2, int_val3; long int longint_val1; char comma; ifstream fs("C:\\a.txt"); if ( !(fs >> char_val >> comma) || comma!=',') { // Bail.... } if ( !fs >> int_val1 >> comma) || comma!=',') { // Bail.... } // etc...
Steve
Steve, Actual items in the file M637.VIN M2,2,1,15,55342,55662,55982,56302 A,4,1,15,58856,59176,59496,59816 P,5,1,15,55327,55647,55967 B,7,1,15,55310,55630,55950,56270 4B,8,2,15,27769,25530 General format: char_val,int_val1,int_val2,int_val3,longint_val1 is it possible to store these stuff in a struct or an array of some sort? Coz i need int_val1 and int_val2 as a reference to an output port. in line 1 "M2,2,1" , i need to access the extracted input 2 and 1 coz it corresponds to an output port2 and port1. on the other hand, char_val must be taken into account coz it serves as a reference to longint_val(n). Thanks, Kuroro
-
Steve, Sorry for the dot stuff, that signifies another input of type long int... actually the first entry is Char, then the second, third and the fourth is of type int, the fifth until end of line is a type of long int. each line have different number of items. I'm not good in explaining things. Thanks Steve
To me, each line looks like a struct constisting of a
std::string
, 3 ints and a std::vector of longints. The constructor of this struct can take a line of the file, parse it and fill the fields.getline
would be the method to - erm - get the line from the file. For parsing, you could use fscanf or look at e.g. www.boost.org[^]for a parsing library.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.