Hello there,
I'm reading lines from a text file in C++ which contains integer + string + float number(like 3,67 with comma) + string in this order. I need the float number to sort the lines but I couldn't manage to separate the data into the types I can use so far. I tried different kind of functions and the best I could do was such a code; - void main (){
-
ifstream records;
-
records.open("records.txt");
-
-
int id;
-
string line;
-
char name[100];
-
float gpa;
-
string depart;
-
if (records.is_open()) {
-
while (!records.eof()) {
-
getline(records, line);
-
istringstream deneme(line);
-
deneme >> id;
-
deneme >> name;
-
deneme >> gpa;
-
deneme >> depart;
-
-
// cout << id << " -- " << name << " -- " << gpa1 << " -- " << depart << " -- " << endl;
-
-
-
}
-
}
-
records.close();
-
system("PAUSE");
-
}
This fails at reading the floating number which has comma in it and then last string is read as string starting with the comma and rest of the number. An output example is:
698 John 3 ,67
It doesn't read last string on the line as well. I understand that part but simply I need another read but what I want exactly is to separate one line using "tab" as a seperator into proper data types and then using the numbers as integers, and the grades as floating numbers. How Can I do this?
5 5485 Rabbit 12,516
Recognized Expert Moderator MVP
c and c++ uses a dot to separate the whole number from the decimal part. Read it in as a string, replace the comma with a dot, and then convert it to a float.
donbock 2,426
Recognized Expert Top Contributor
Could the string in the middle column ever contain a space?
To Rabbit's point, if the file is small, and you don't need an ongoing solution, just load the file into notepad and replace "," with "."
You might want to try the Unix/Linux utility Awk. MS Access has a good import Wizzard that will make these problems very manageable, and you you'll have a trial & error environment to see if you guessed right. Once in Access you have lots of output options - like .csv
Not sure if it will help or not, but if you need to replace the ',' with a '.', then maybe -
long pos;
-
-
getline(records, line);
-
-
if((pos = line.find(",")) != string::npos)
-
{
-
line.at(pos) = '.'; // replace the comma
-
--pos;
-
while(isspace(line.at(pos)))
-
{
-
// remove any spaces if need be
-
line.erase(pos, 1);
-
--pos;
-
}
-
}
-
-
isstringstream dename(line);
-
...
just check the output before actually trying to use the string stream
Can you make a struct containing member variables that map the data types you read when you parse a line.eg (int, string, float).
after you read a line (in getline function in your code) create a new struct of this type and implement conversion of data to member variables in its constructor(argument for constr will be string). Inside constructor you can use string tokenizer to parse string and assign appropriate values in member variables. I dont think this should be difficult. After creating the struct implement a predicate so you can sort the struct as per your need. Hope this helps.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Stu Cazzo |
last post by:
I have the following:
String myStringArray;
String myString = "98 99 100";
I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it...
|
by: |
last post by:
Hi,
I need to read a big CSV file, where different fields should be converted to
different types,
such as int, double, datetime, SqlMoney, etc.
I have an array, which describes the fields and...
|
by: sathyashrayan |
last post by:
Group,
I have some doubts in the following program.
------------------program---------------------
/*
** Make an ascii binary string into an integer.
*/
#include <string.h>
unsigned int...
|
by: El Camino |
last post by:
I need to switch a string to an integer
if its strLetter = "A"
integer value of 1
if its strLetter = "B" then
integer value of 2
if its strLetter = "C" then
integer value of 3
|
by: Pete |
last post by:
Only just started using VB and I've got a problem, not sure how to
solve this one.
I'm receiving a string from a comm port containing hex values which
need to be converted into characters using...
| |
by: arnaudk |
last post by:
Hello,
I am relatively new to C++ and have been milling over a container related problem for a while now. I have a ~2Gb ASCII file of time-ordered data with 4 comma-separated columns. The first...
|
by: skanemupp |
last post by:
which is the best way to check if a string is an number or a char?
could the 2nd example be very expensive timewise if i have to check a
lot of strings?
this
value = raw_input()
try:...
|
by: dmitrey |
last post by:
hi all,
howto split string with both comma and semicolon delimiters?
i.e. (for example) get from string "a,b;c"
I have tried s.split(',;') but it don't work
Thx, D.
|
by: N9 |
last post by:
Hi
Anyone who can help about split string.
string text = "History about a boy, who loves to play baseball with
his friends."
I like to find indexOf "play" and read the string 10 char left...
|
by: Daniel DePasqua |
last post by:
I do not understand the concept of inputing a number and having the program convert it to an integer, float and string. How would you write a program that does that
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| | |