Read white spaces in in a file C++ [duplicate] - c++

This question already has answers here:
Reading from ifstream won't read whitespace
(10 answers)
Closed 4 years ago.
How I can read line of a file with a whitespace, in the case that faddress was a string with space I will not able to read all the line.
What I can do ? I try using noskipws but it doesn't work
http://www.cplusplus.com/reference/ios/noskipws/
Code:
ifstream rFile;
rFile.open("list_of_people.txt");
while(rFile >> fname >> flastName >> ocupation>> faddress >> age){
insert(fname,flastName,ocupation,faddress,age);
}
File :
John Smith Engineer Av Roses 25

You could use std::getline() to have the entire line in a string, then use string functions to parse it / tokenize it as you see fit. About doing that, see, for example:
Parse (split) a string in C++ using string delimiter (standard C++)

Related

Replace text \n with actual new line. (C++) [duplicate]

This question already has answers here:
How to find and replace all occurrences of a substring in a string?
(9 answers)
Replace substring with another substring C++
(18 answers)
How to find and replace string?
(11 answers)
How do I replace all instances of a string with another string?
(6 answers)
Closed 6 months ago.
I'm using C++ and I have a problem. Instead of creating a new line it prints \n. My Code:
std::string text;
std::cout << text;
It prints:Hello\nWorld
It was supposed to read \n as a new line and print something like this:
"Hello
World"
So i've tried to use replace(text.begin(), text.end(), '\n', 'a') for testing purposes and nothing happened. It contiuned to print Hello\nWorld
std::replace() won't work in this situation. When called on a std::string, it can replace only single characters, but in your case your string actually contains 2 distinct characters '\' and 'n'. So you need to use std::string::find() and std::string::replace() instead, eg:
string::size_type index = 0;
while ((index = str.find("\\n", index)) != string::npos) {
str.replace(index, 2, "\n");
++index;
}

how to get specific data from a txt file in c++ [duplicate]

This question already has answers here:
use getline and while loop to split a string
(3 answers)
Closed 1 year ago.
it's my frist publication and i have a question-
I have a .txt file that I want to separate by the ",".
Example:
example1,example2,example3,example4
I want to get example1 and store in an array and so on for example2 , example3, example4.
string[0] = example1
string[1] = example2
etc....
getline by default will split input by end-line chatracter, but can do for any character, including ',' - see example (using a stringstream):
std::string input = "word1,word2,word3";
std::stringstream sstream(input);
std::string word;
while(std::getline(sstream, word, ',')) {
std::cout << word << '\n';
}

C++ - How can i split up 3 lines safed in string called "result" and create a oneline "synonym" for all 3 strings. - C++ [duplicate]

This question already has answers here:
Parse (split) a string in C++ using string delimiter (standard C++)
(33 answers)
Closed 2 years ago.
So, Hello Guys.
I have a function, which reads the Diskdrive Serialnumber.
I get a output like this:
SerialNumber A6PZD6FA 1938B00A49382 0000000000000
(thats not my serialnumber, just the format)
So now, i want to split the 3 numbers, or strings, however i call it you know what i mean, and save it in in three independent strings.
string a = {A6PZD6FA this value} string b = {1938B00A49382 this value} string c = {0000000000000 this value}
After that, i want to create a oneline "synonym" for all 3 strings. So i mean,
string synonym = 04930498SV893948AJVVVV34
something like this.
If you have the original text in a string variable, you can use a std::istringstream to parse it into constituent parts:
std::string s = "SerialNumber A6PZ etc etc...";
std::istringstream iss{s};
std::string ignore, a, b, c;
if (iss >> ignore >> a >> b >> c) {
std::string synonym = a + b + c;
...do whatever with synonym...
} else
std::cerr << "your string didn't contain four whitespace separated substrings\n";

Read elements from a txt file that are separeted by a character

I'am working on a program where there are first names, last names and a numbers on a file and i need to read that information into my program. My actual problem is that there is people who doesnt have a second name or second last name. To solve the issue I started trying to read from a file until a specific character is found, For example:
Robert, Ford Black,208 //Where Robert is the first name, and Ford Black are his two last names
George Richard, Bradford,508 //Where George Richard are both his first names, and Bradford is his only last
name
I am saving this information in three separeted string, one that will store first and second name, first last and second last name and the third one for the numbers.
I'm trying to only use native libraries from c++.
I've been reading that getline(a,b,c) and IStringStream can actually solve my problem but I don't know how to correctly implement it
It's just a matter of using std::getline with a delimiter character to read out of the string stream. See a simplified example (no error checking) below:
for (std::string line; std::getline(std::cin, line); )
{
std::string firstName, lastName;
std::istringstream iss(line);
std::getline(iss, firstName, ','); // A comma delimits end-of-input
iss >> std::ws; // Skip over any whitespace characters
std::getline(iss, lastName); // Read remaining line
std::cout << "First Name: " << firstName << std::endl;
std::cout << "Last Name: " << lastName << std::endl;
}
Note the line iss >> std::ws; using std::ws from <iomanip> is there to eat up extra whitespace characters (which appear after your comma, in your example).
I'm assuming the C++ line comments in the input are only an annotation for this question, and not part of the actual input.
#include<bits/stdc++.h>
using namespace std;
int main()
{
ifstream myfile("files.txt");
string fullname;
while(getline(myfile,fullname,'/')) break; //here im reading till the first / is acquired and the entire string is stored in "fullname"
string firstname,lastname;
size_t pos=fullname.find(',');
firstname=fullname.substr(0,pos); //store the firstname
lastname=fullname.substr(pos+1);// storee the lastname
cout<<firstname<<" "<<lastname;
}
As the question posed was to read names im assuming before the digit if there were a " / " you can read upto the first occurance of /. this will give you the fullname. Then using the substr on the fullname and find the occurance of a comma if at all it exists. All the characters to the left of position of comma will form your first name and the rest on the right of the position of comma will form the lastname.

Read a file line by line and insert data in to variables and arrays [duplicate]

This question already has answers here:
Read file line by line using ifstream in C++
(8 answers)
Closed 3 years ago.
VA301
20/02/2020 10:20
COLOMBO
SINGAPORE
10 E AB
15 E CDE
22 E ADF
31 E BCF
35 E ABCD
45 E AB
50 E DEF
These are the details in my file. I want to read this file line by line and store 1st 5 lines into a variable and other lines into 3 char arrays.
I don't know why you really want to do so. If you can give me a better explanation, I can give you a better answer.
To read form file you have to use a file stream input.
Example:
ifstream infile("thefile.txt");// change thefile to your file name and make sure it's at the same folder with the programe
Now you can use getline() method to get data from the stream.
string line;
char ch[200];
getline(infile, line);//this to store the line into a string
getline(infile,line,'&'); // the last parameter is the "delimiter"
//getline() will use delimiter to decide when to stop reading data.
infile.getline(ch,200); //this to store the line into a char array
And simply you can read to the of the file using a loop and eof() method
while (infile.eof( ))//Mean read until the end of file
{
//do something
}
To get everything together:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream infile("thefile.txt");// change thefile to your file name and make sure it's at the same folder with the programe
string line, var="";
while (infile.eof( ))//Mean read until the end of file
{
getline(infile, line);//this to store the line into a string
var= var + line +'\n';
}
//assuming that they are just 3 other lines
char ch1[200],ch2[200],ch3[200];//you can choose another size
infile.getline(ch1,200);
infile.getline(ch2,200);
infile.getline(ch3,200);
}
For more information you can read:
https://en.cppreference.com/w/cpp/string/basic_string/getline
https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm