I'm trying to read this file and store each row in one array. Can someone please tell me how to implement the code for this?
2
2 10 1 2 7
3 8 3 7 7 10 7
I have the code below and I can save every element of the text file into an array, but I need to have the lines saved into seperate arrays. How do I do this?
fstream myfile("myfile's_address", ios_base::in);
int a;
while (myfile >> a)
{
word[increment] = a;
increment++;
}
First, use ifstream instead of fstream with ios_base::in.
Next, use std::getline() to get one line as a string, create one vector for it (perhaps in a vector<vector<string>>), then parse it (perhaps using std::istringstream).
Related
I was given a question where the input will be like:
10 8
4 9
6 12
5 4
3
1
Here I don't know the number of lines that contains 2 integers. Those sets of 2 integers will be taken into an array. But when the program encounters "3", it will start taking input in another array.
I have tried this with
while(cin>>a>>b){ //some porcess with a and b }
but it doesn't work because it recognizes 3 and 1 as another set of two integers. Please help me to solve this problem.
cin >> a >> b skips not only spaces, but any delimeter characters too ('\n', '\t', ' ').
Here you actually may want to read input line-by-line and then check if there are two integers or one. Consider use of std::getline for retrieving each line of text. Then you can use read string as std::istream (like in example in the link above) and read from it with counting, how many numbers you read totally.
So think about your problem. Essentially it is, read one line at a time, and if it contains two numbers do one thing, but if it contains one number do something else.
But the code you have written reads numbers not lines. That is where the problem is.
Instead write your code to read only line at a time, analyse that line to see if it contains one or two numbers (or something else) and then proceed from there.
What you need is the ability to read a line of text into a string, and then read from that string into your numbers. To do that you use an istringstream. Something like this
#include <iostream>
#include <sstream>
#include <string>
int a, b;
string s;
getline(cin, s); // read one line from standard input
istringstream line(s); // put that string to a stream we can read from
if (line >> a) // try and read the first number from the stream
{
// got the first number
if (line >> b) // try and read the second number from the stream
{
// got the second number
...
}
else
{
// only one number
...
}
}
else
{
// didn't get any numbers, some sort of error
...
}
So, lets say we have a text.txt file with these numbers:
4 5 15 10 20
5 5 15 10 20 25
In the above example, the first numbers in the row describe how many numbers are in that row. The rest of the numbers are the numbers I am interested in (I will be sorting them in a later part of the code, but that is not where my question focuses).
My issue is, how can I best go about taking each row of numbers (ignoring the first number), placing them into a array, and then moving onto the next line and doing the same thing (placing them into an array, that will be later sorted)?
All my google searching points to doing this with strings via getline, and nothing really points to handling it with ints. Hope someone on here can help point me in the right direction.
Below is the basic code I would use to open the file:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a;
ifstream inputfile;
//declare an input file
inputfile.open("text.txt");
while(//not sure best way to do this part)
{
//guessing I can use a for loop and place numbers in array
//based on first number in the row of numbers
}
return 0;
}
The most obvious way would probably be to read a line with std::getline, then put the string into a stringstream, and read numbers from there (ignoring the first, obviously).
I suggest the following approach:
std::string line;
// Keep reading lines of text from the file.
// Break out of the loop when there are no more lines in the file.
while (getline(inputfile, line)){
// Construct a istingstream from the line of text.
std::istringstream ss(line);
// Read the numbers from the istingstream.
// Process the numbers as you please.
}
I'm trying to create a function where it allows the user to type in multiple amounts of integers, so if the user wanted to have 3 different storages that hold different integers, the input would look something like this:
5
97 12 31 2 1 //let's say this is held in variable "a"
1 3 284 3 8 // "b"
2 3 482 3 4 // "c"
2 3 4 2 3 // "d"
99 0 2 3 42 // "e"
Since we don't know what number the user will input every time, I'm not sure how to create a dynamically allocated array that will create an x amount of arrays every time.. I want to be able to access each index of a, b, c, d, e or however many arrays there are.
So far, this is what I have, but I'm having trouble creating the arrays since it's unpredictable. I'm purposely not using vectors because I don't really get how pointers work so I'm trying to play around with it.
int* x;
int length, numbers;
cin >> length;
x = new int[length]
for (int i=0;i<length;i++)
{
std::getline(std::cin, numbers); //this didn't work for me
x[i] = numbers
}
If anything seems unclear, please let me know! Thank you!
It doesn't get the first line. It gets 1 integer at a time and since you have 5 integers per line and you entered 5 in the first line you end up getting only the numbers in the first line. x in your code is an array of integers and it needs to have enough place for all your integers which in this case is 25. If 5 integer per line is guaranteed then you can assume allocating 5 * length integer-long place will work. You will also need an inner for loop. 1 for to loop through lines and another one to loop through every integer on a line.
I would suggest using cin like so:
int d;
while(cin){
cin >> d;
// Do something with d
if(cin.peek() == '\n'){
// Create a new row in your dynamic array
}
}
This will grab each digit up to the space.
Another way to achieve this is by using strings with getline() in conjunction with string.empty() to get each line, then using strtok to split the line up into tokens. Although getline only works on strings, strtok will split the string up into tokens, which you can then cast to an int (or use atoi).
To store these tokens you will want to use a vector, since they are dynamic by nature and can easily be resized to fit any need. I would see this discussion on multi-dimensional vectors.
This question already has answers here:
Read integers from a text file with C++ ifstream
(2 answers)
Closed 10 years ago.
Let's say I have a .in file with the first line of data "3 59 98" and the second line of data "8 52 77 45".
I'm trying to read each line of integers into a list. I already understand how to make a list and put numbers in the list. What I am having trouble with is how to get the first line of numbers into a list object.
the following is an idea of what I have so far:
// in is a filestream object
int a
while (in >> a)
{
integer_list.push_back(a);
}
I know this doesn't work because it puts both lines of numbers into one list.
Any suggestions?
Use getline to read a whole line and then create std::istringstream from this line. Read from the std::istringstream the numbers in the list just as if you are reading from a file stream.
What you need to do is to create a list of integer lists, although in C++ you would prefer to use a vector.
std::vector< std::vector<int> > list_of_integer_lists;
then for each line you would add a new list or vector.
list_of_integer_lists.push_back( std::vector<int>() );
and for each line you add the numbers to the last list.
list_of_integer_lists.back().push_back( number );
i am creating a program to read the .dxf file of autodesk.
i am encountering a problem while reading strings.
when i use :
string acad;
fstream f;
f.open(name);
f >> acad
if the string is "chamfer" it works perfect.
but if the string is "a & b" it is able to read only upto a.
since the file format follows a pattern i am using while loop.
example from a file:
9 //loop 1
$DWGCODEPAGE //loop 1
3 //looop 1
ANSI_1252 //looop 1
9 //loop 2
$LASTSAVEDBY //loop 2
1 //loop 2
sam & tom //loop 2
9 //loop 3
$INSBASE //loop 3
10 //loop 3
0.0 //loop 3
as you can see sometimes there may not be any space as in "ANSI_1252" & sometimes there may be spaces as in "sam & tom".
how can i generalise the code so that the whole string in a line is stored along with the spaces, if any.
please forget about the spaces in the beginning of each line, i am using ws for that.
thank you!
>> operator reads words delimited by space, when used with a string parameter. If you want to read lines of characters, you should use getline() instead.