Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a file called file.txt
1 2 3
ugjfnuwd
gjufjfg
and I want to extract the first line of this file "1 2 3" and turn it into an array in c++ so the end result would look something like this
[1, 2, 3]
I've been experimenting and researching for around 2 hours with little progress. Please help
I would read the line into an std::string, then use an std::istringstream to parse the integers out of the line, something on this general order:
// open the input file:
std::istream infile("file.txt");
// read in the line
std::string line;
std::getline(infile, line);
// put the line into a stringstream
std::istringstream parser(line);
// initialize the vector from the numbers in the stringstream:
std::vector<int> numbers{ std::istream_iterator<int>(parser), {} };
// print out the result, one number per line:
for (int i : numbers)
std::cout << i << "\n";
Read the first line into a string like normal and then split the string into an array with the delimiter being the space character.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
Given a string we have to output the string in a special way.
• If the string consists of one char, we output that char normally.
• Otherwise, we divide the string into two equal parts (if the number of letters in the substr is odd, the second part of the substr will be one letter longer than the first), and we output the first part twice and then the second part (according to the same rules).
For example, let's assume that we want to output the string YOGURT. We divide that string into two equal parts: YOG and URT.
How will we output the substr YOG? Again, it will be divided into two parts - Y and OG. The substr Y we output normally (but in the output of the substr YOG we will do it twice), and the substr OG we output as OOG. So the substr YOG we output as YYOOG.
Analogously, the substr URT is going to give the output UURRT. So the string YOGURT is going to be output as YYOOGYYOOGUURRT.
Length of the string can at max be 10000.
Now I tried using a non recursion way to solve this problem but it was way to slow so I have come to an conclusion I have to do this with recursion. And since I don't have that much experience with recursion I would really need some help.
This is very naturally implemented with recursion like so:
void print(std::string_view s) {
if (s.size() <= 1) std::cout << s;
else {
auto m = s.size() / 2;
print(s.substr(0, m));
print(s.substr(0, m));
print(s.substr(m));
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I entered three inputs to the code below and got the results as follows
(1)
input: CTRL+D
result: (blank)
(2)
input: abcCTRL+D
result: (not terminated yet)
(3)
input: abc
result: abc
using namespace::std;
int main()
{
string input;
cin >> input;
cout << input << endl;
return 0;
}
I wonder why I should enter EOF twice to terminate the code in the second case
not just only one time like the first case (it terminated immediately)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an assignment like this:
/*
- Read an input text file (.txt) contain one line to store an array of integer:
Input.txt
4 1 2 -100 -3 10 98 7
- Write SumList function to sum all integer data of the list
- Write a function to find the max of all integer data
- ...
*/
My question is how to count the number of numbers in the txt file to use
/for (int i = 0; i < N; i++), N is number of numbers in file/ for reading the file. Or is there any way else to read this file without initializing N?
Thank you!
Your real question is: how to read a file word by word.
I believe that you've known what file stream is, so here is the code:
fstream file("yourfile.txt", ios::in);
std::string word;
while (file >> word)
{
// convert word to int
}
Now the next question is: how to convert a string to int. I hope you can figure it out on your own --- http://www.cplusplus.com/reference/cstdlib/atoi/
Also, this would be easier: (Thanks to #Fei Xiang)
int i;
while (file >> i)
{
// do something
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I've been playing around with C++ (just starting out), and I'm trying to make a program that needs a function to count the lines in C++. However, I've encountered weird behavior where normal assignment doesn't work, but assignment through address of, and then immediate dereference does. Like this:
int countLinesInFile(string fileName){
char c[32];
int numLines = 0;
ifstream file(fileName);
while(file >> c){
numLines += 1;
cout << "Lines: " << numLines << endl;
}
return numLines;
}
Which results in:
Lines: 1
Lines: 1
Lines: 1
Lines: 1
Lines: 1
Lines: 1
However, when I change numLines += 1 to *(&numLines) += 1 it magically works:
Lines: 1
Lines: 2
Lines: 3
Lines: 4
Lines: 5
Lines: 6
For a little background, the file I'm reading is a 6 line file where each line is a 32 bit binary string (equal to zero). When I print out c (with cout << c) it prints out seemingly correctly. Also, I am aware that this may not be the optimal or correct way of doing read lines from a file, but unless this simply can not possibly work, I am more interested in the underlying mechanics of why this behavior is happening, and what I am doing wrong.
'\0' character binary value is 00000000. I guess that numLines is just after your array c in memory and is always erased with zeros, before being incremented by 1. So it always displays as 1.
When you compile the second version, the memory must be organized another way (only the compiler knows, and you if you debug at very low level), which does not impact numLines value.
Well, here's the answer. As StoryTeller said, the array must be 33 characters long in order to capture the terminating character and prevent the unusual behavior.
P.S. Thank you for all the helpful comments, and I agree that getline is probably a better alternative. If anyone has any insight as to how exactly this overflow might cause this weird behavior that would be very welcome.
Use class std::string with the API std::getline where you can read a whole line at each read and increment at each read the counter variable:
std::string sLine;
std::ifstream in("main.cpp");
int nLines = 0;
while(std::getline(in, sLine))
++nLines;
std::cout << nLines << endl;
in.close();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a problem to read a large int ( 1 to 10^100 ) into vector the problem is I cannot read it as numeric data-type and split it into the vector so I want a solution to read the number separately into the vector
Example:
45686469
vec[0] = 4
vec[1] = 5
...
vec[7] = 9
Here's one possible way to do it:
std::string yourinput;
cin>>yourinput; //capture your large number as a string
std::vector<char> vch;
for(size_t st=0;st<yourinput.length();++st)
{
vch.push_back(yourinput[st]); //move each character into the vector
}