Count the number of elements in txt file [closed] - c++

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
}

Related

Convert first line of file into array C++ [closed]

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.

CodeBlocks C++ Display how many numbers i have typed in [closed]

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 years ago.
Improve this question
I'm trying to make it, so lets say i type in 654321, it would say that i typed in 6 numbers.
I need to make it so it counts how many numbers i have typed in, and would display so.
Looking for anyone who could do that for me, thanks in advance.
Considering your entered number is an integer, you can setup a counter variable to count the number of digits and then divide the number by 10 and subsequently increment count in a loop:
#include <iostream>
int main()
{
long long num;
int count = 0;
std::cin>> num;
do
{ count++;
num /= 10;
} while(num != 0);
std::cout<< count;
}
Use long long for large input.
If your entered number is a string, then you can use stoi() to convert it into an integer.

Small thing about input using while loop [closed]

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 hope you guys all are having a great day!
I have a quick question about using the while loop for competitive programming (we do not know the size of the input, so we have to read until the end of file or 0 value)
For this particular program, the program end with 2 values of 0 as "0 0", and the code I saw used this:
while (cin >> r >> n, r || n) {
// code
}
My question is about the >>> , r || n <<<< part:
Is the while loop as the same meaning as
while ( (cin >> r >> n) || (r || n) )
can I have some preferences to read more about the multi conditions for the while loop.
Please regard my dump question :( Tks you all for reading this post!
Basically.... comma has the lowest precedence and is left-associative.
Given A , B
A is evaluated
The result of A is ignored
B is evaluated
The result of B is returned as the result.
Further Reading : https://stackoverflow.com/a/19198977/3153883
So in your case, cin loads r and n. The return value from that operation is ignored. r or n happens and is the result of the whole while expression. So, a 0 0 will cause the while loop to terminate.

Efficient way to count ints in line C/C++ [closed]

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 6 years ago.
Improve this question
I've been tasked with solving a rather simple problem but I'm stuck on 1 thing
Given the following file structure:
4
124 123 145 152
1400
13 23 51 24 1412 5151 52512 12412...
...
I am supposed to do various tasks with data but before doing anything with it I have to check whether int in first line corresponds to number of ints in 2nd line (same for 3rd and 4th line)
The problem is the number of elements can reach up to 150 000 and int value is between (1;2 000 000) so using getline can be problematic (afaik) as it will be resource heavy.
Cin will ignore whitespaces and \n so I will never know when line ends
I can't modify the original file and I try to use as least resources as possible.
hm not sure if i understand the question completely but why dont you use getline to store the line that contains 1400 ints as a string. Afterwards using a istringstream make sure you read 1400 ints. Maybe something like this:
getline(cin, line);
while(iss >> n && i++ < numOfInts) {}
then outside make sure i == numOfInts.
This way you'll never actually be storing 1400 ints, you'll just keep overwriting the previous integer
You can create a custom function to take the integer values, Something like this:
int getints(){
int ctr = 0;
char ch;
int sign = 1, n=0;
while(ch!='\n'){
while(ch==' ' || ch=='\t')
ch = getchar();
if(ch=='-'){
sign = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
n = n*10 + ch - '0';
ch = getchar();
}
n *= sign;
++ctr;
}
return ctr;
}
This function will return the number of integers scanned in a line. Just check it with the number of integers that are supposed in a line. It basically skips all whitespace characters and terminates input after newline is found.

how to condense a string of numbers [closed]

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 7 years ago.
Improve this question
char *s = "1234560000000000078999";
unsigned int ss = strlen(s);
vector<int> num;
unsigned int i;
for (i=0;i<ss;i+=2)
{
num.push_back((s[i] - '0')*10 + (s[i+1] - '0'));
}
i'm trying to condense a string that only contains numbers and store it in a int vector
the idea is to take each couple of numbers int the string and combain them into one integer
the problem i had is with numbers that start with zero , for example 1107 only gets stored as 117 and 1100 as 110
the other problem i had is with even numbers ;
any sultions please
thank you
1107 does, indeed, get stored as 11 and 07. When you display the values, show two digits or you won't see the leading 0 on the 07. Same thing with 1100.
As to even numbers, yes, you have to look more carefully at the number of digits that you're dealing with. If ss is odd, start out by just storing the first digit. Then process the rest in pairs. So 117 would be stored as, essentially, 01 and 17.