Reading Input From File and Storing into 2D Array - c++

I need to read in a text file that contains only integers and each one is separated by new line. example would be:
0
1
2
...
64
repeating 0 to 64 64 times
Essentially the file is 64*64 lines long, containing an integer for each line.
I need to store each integer (line) in ldisk, my 2D array, but am having serious problems doing so. I understand my code has an error because I am trying to store a string in a char, but I am not sure how to get around this. By the way, ldisk must be a 2-D array of chars. I would love some advice/feedback on my current code posted below, or an alternative solution.
NOTE: I am a beginner at C++
PS:
I know similar topics exist, but mine is more to the problem of getting around the type conversion or just converting it properly so I can store more than a single digit integer into my 2D array, because I have it working where I can store only the first digit where I want in my 2D array, but run into problems if there is more than 1 digit.
int main(){
char **ldisk;
ldisk = new char*[64];
for (int i = 0; i<64; i++)
{
ldisk[i]= new char[64];
}
int counter = 0;
string line;
ifstream inFile("example2.txt");
while ( getline(inFile, line))
{
int first, second;
first = counter/64;
second = counter%64;
cout << line;
ldisk[first][second]= line;
}
return 0;
}
EDIT:
My apologies I have no idea how to do a table.
I want ldisk[0][0] to be 0,
then ldisk[0][1] to be 1,
then ldisk[0][2] to be 2,
etc,
etc,
then ldisk[0][63] to be 64
Eventually it will fill up such that ldisk[63][63] = 64

This is the problem:
ldisk[first][second]= line;
The type of ldisk[first][second] is char. You are trying to assign a std::string to it.
You can make your life a lot simpler by using a std::vector<std::string>.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main(){
vector<string> ldisk;
int counter = 0;
string line;
ifstream inFile("example2.txt");
while ( getline(inFile, line))
{
cout << line;
ldisk.push_back(line);
}
return 0;
}
Update
If you must have char** ldisk, you can change main to:
int main()
{
char **ldisk;
ldisk = new char*[64];
for (int i = 0; i<64; i++)
{
ldisk[i]= new char[64];
}
int counter = 0;
string line;
ifstream inFile("example2.txt");
while ( getline(inFile, line) && counter < 64 )
{
cout << line << endl;
if ( line.size() >= 64 )
{
cout << "Line is longer than 63 characters. Copying only 63 characters from it.\n";
strncpy(ldisk[counter], line.c_str(), 63);
ldisk[counter][63] = '\0';
}
else
{
strcpy(ldisk[counter], line.c_str());
}
++counter;
}
return 0;
}

Change your loop to:
for (counter = 0; counter < 64*64; ++counter)
{
int item;
if ( !(inFile >> item) )
{
cerr << "File only contained " << counter << "items.\n";
return 1;
}
if ( item < CHAR_MIN || item > CHAR_MAX )
{
cerr << "Item " << counter << " invalid value " << item << "\n";
return 2;
}
ldisk[counter/64][counter%64] = item;
}
The missing ingredient is that you were not trying to convert the string in your file into an integer value. You may need #include <climits>.

Related

Getting a line of string and converting into a float list

I need to achieve the following:
"4 5 1.3 0 3.1"
This is the input string that I will read from the user, after reading I need to turn this string into a float list according to the [0]th character's size for example the list will be
array[4] = [5.0,1,3,0.0,3.1]
How can I achieve it I tried using getline but didnt work. Thanks in advance.
string line = "4 5 1.3 0 3.1"; // input string
istringstream stream(line); // parser
unsigned count; // how many values
double* values;
if (stream >> count) {
values = new double[count];
for (unsigned ii = 0; ii < count; ++ii) {
stream >> values[ii]; // read one
if (!stream) {
throw std::runtime_error("not enough numbers");
}
}
} else {
throw std::runtime_error("no count at start of line");
}
// do something with values...
delete[] values;
First read the count into an unsigned integer, then loop from 0 to the count, reading into doubles. To read into an array, first allocate one dynamically with the correct size. Better, use a std::vector, which will handle the allocations for you.
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
int main() {
std::string line;
std::getline(std::cin, line);
std::istringstream ss(line);
size_t num_values = 0;
if (! (ss >> num_values) ) {
std::cerr << "Could not read the integer number of values!\n";
return 1;
}
auto values = std::make_unique<double[]>(num_values);
double tmp;
for (size_t idx = 0; idx < num_values; ++idx) {
if (! (ss >> values[idx])) {
std::cerr << "Could not convert the " << idx << "th value!\n";
return 1;
}
}
// Validate solution: print the contents of the vector
for (size_t idx = 0; idx < num_values; ++idx) {
std::cout << values[idx] << " ";
}
std::cout << "\n";
}
[live example]
This solution uses a dynamically allocated array, and ensures that its memory is correctly cleaned up by wrapping it in a std::unique_ptr, created via a call to std::make_unique.

How do you flush the contents of `std::cin` before an additional read from it?

I am trying to read a single character multiple times. The catch is that I need to prevent user errors. So for example:
char arr[10];
for(int i = 0; i < 10; i++)
{
cin.get(arr[i]);
}
Where the inputs should be something like a, b, c, d, .... But if someone were to enter ab for the first entry I want to capture the a and then ignore the b. I know about cin.ignore however I don't know how I would go about ignoring an arbitrary number of alphanumeric characters or symbols considering that I want to ignore a potentially unlimited number of characters and then stop ignoring and read again.
How can I either ignore an arbitrary number of characters and then stop ignoring or how can I actually flush the buffer for cin.
Most input is line feed so if you want to ignore all characters in the input stream until you hit a newline then you could use:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
Since we ignore up to the streamsize there should not be an extra content in the input buffer.
If you want user to hit enter after each symbol, then code could be as simple as this:
char arr[10];
for(int i = 0; i < 10; )
{
std::string line;
std::getline( std::cin, line );
// check that line is not empty
if( line.empty() ) {
std::cout << "missing input" << std::endl;
continue;
}
arr[i++] = line[0]; // get only first symbol and ignore the rest
}
if you have something else in mind, I am afraid that will not work with std::cin - you do not see any input until user presses enter. In that case you would have to use OS specific functions to get unbuffered input.
The following is the code that you want, if your inputing like this a 'enter' b 'enter' c 'enter' etc...
#include <iostream>
#include <string>
using namespace std;
int main() {
char arr[10];
string line;
for (int i = 0; i < 10; i++)
{
getline(cin, line);
arr[i] = line[0];
cout << endl << "Here is the Char: " << arr[i] << endl;
}
return 0;
}
BUT if you enter input like this in one line: a,b,c,d,e,f,g,h,i,j 'enter' then you want the following code:
#include <iostream>
#include <string>
using namespace std;
int main() {
char arr[10];
string line;
int i = 0;
size_t end;
getline(cin, line);
end = 0;
int counter = 0;
if (line != "") {
while (end != string::npos && counter < 10) {
if (counter == 0) {
arr[counter] = line[0];
}
else {
end = line.find(",", end + 1);
arr[counter] = line[end + 1];
}
counter++;
}
}
for (int i = 0; i < 10; i++) {
cout << endl << "Here is the Char: " << arr[i] << endl;
}
return 0;
}

How to read lines of text from file and put them into an array

I created a text file love.txt:
i love you
you love me
How do I store them into separate array, namely line1 and line2 and then display them out in console?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line1[30];
string line2[30];
ifstream myfile("love.txt");
int a = 0;
int b = 0;
if(!myfile)
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while(!myfile.eof())
{
getline(myfile,line1[a],' ');
cout<<"1."<<line1[a]<<"\n";
getline(myfile,line2[b],' ');
cout<<"2."<<line2[b]<<"\n";
}
}
Try specifying the last argument as '\n' in both getline() functions:
getline(myfile, line1[a], '\n');
instead of
getline(myfile, line1[a], ' ');
How about this.. .
vector <string> v;
string line;
ifstream fin("love.txt");
while(getline(fin,line)){
v.push_back(line);
}
You can think of a string as an array of characters, so you will only need one array of strings:
const size_t SIZE = 30;
string line[SIZE]; // creates SIZE empty strings
size_t i=0;
while(!myfile.eof() && i < SIZE) {
getline(myfile,line[i]); // read the next line into the next string
++i;
}
for (i=0; i < SIZE; ++i) {
if (!line[i].empty()) { // print only if there is something in the current line
cout << i << ". " << line[i];
}
}
You could maintain a counter to see how many lines you have stored into (instead of checking for empty lines) as well -- this way you will properly print empty lines as well:
const size_t SIZE = 30;
string line[SIZE]; // creates SIZE empty strings
size_t i=0;
while(!myfile.eof() && i < SIZE) {
getline(myfile,line[i]); // read the next line into the next string
++i;
}
size_t numLines = i;
for (i=0; i < numLines; ++i) {
cout << i << ". " << line[i]; // no need to test for empty lines any more
}
Note: you will be able to store only up to SIZE lines. If you need more, you will have to increase SIZE in the code. Later on you will learn about std::vector<> that allows you to dynamically grow the size as needed (so you won't need to keep track of how many you stored).
Note: the use of constants like SIZE allows you to change the size in one place only
Note: you should add a check for errors in the input stream on top of eof(): in case there was a read failure other than reaching the end of the file:
while (myfile && ...) {
// ...
}
here myfile is converted to a boolean value indicating if it is OK to use it (true) or not (false)
Update:
I just realized what you are after: you want to read the input as series of words (separated by space), but display them as lines. In this case, you will need arrays-of-arrays to store each line
string line[SIZE1][SIZE2];
where SIZE1 is the maximum amount of lines you can store and SIZE2 is the maximum amount of words you can store per line
Filling this matrix will be more complex: you will need to read the input line-by-line then separate the words within the line:
string tmp; // temporary string to store the line-as-string
getline(myfile, tmp);
stringstream ss(tmp); // convert the line to an input stream to be able to extract
// the words
size_t j=0; // the current word index
while (ss) {
ss >> line[i][j]; // here i is as above: the current line index
++j;
}
Output:
for (i=0; i < numLines; ++i) {
cout << i << ". ";
for (size_t j=0; j < SIZE2; ++j) {
if (!line[i][j].empty()) {
cout << line[i][j] << " ";
}
}
}
My complete solution.
the config.txt file contains:
#URL WEB POP3 IMAP SMTP FTP DNS PING ORDER
st-xxxx.com 1 1 0 1 1 0 1 1
24.xxx.195.11 1 1 0 1 1 0 1 2
24.xxx.195.12 1 1 0 1 1 0 1 3
192.168.0.100 1 1 0 1 1 0 1 4
and my code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
ifstream myfile("config.txt");
if(!myfile)
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
const size_t SIZE1 = 30;
const size_t SIZE2 = 10;
string line[SIZE1][SIZE2];
string tmp; // temporary string to store the line-as-string
size_t i=0; // the current line index
size_t j=0; // the current word index
while(!myfile.eof() && i < SIZE1) {
getline(myfile, tmp);
stringstream ss(tmp); // convert the line to an input stream to be able
//to extract the words
size_t j=0;
while (ss) {
ss >> line[i][j]; // here i is as above: the current line index
++j;
}
i++;
}
size_t numLines = i;
cout << numLines << "\n";
for (i=1; i <= numLines; ++i) {
for (size_t j=0; j < SIZE2; ++j) {
if (!line[i][j].empty()) {
cout << line[i][j] << " ";
}
}
cout << "\n";
}
cout << line[3][0] << "\n"; // print the third line first word
}
Hope tht helps anybody who is searching for this type of solution, even if the post is quite old.

C++ program to read a txt file and sort data

I'm a physics PhD student with some experience coding in java, but I'm trying to learn C++.
The problem I'm trying to solve is to read in data from a .txt file and then output all the numbers > 1000 in one file and all those <1000 in another.
What I need help with is writing the part of the code which actually reads in the data and saves it to an array. The data itself is only separated by a space, not all on a new line, which is confusing me a bit as I don't know how to get c++ to recognise each new word as an int. I have canabalised some code I have got from various sources online-
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include<cmath>
using namespace std;
int hmlines(ifstream &a) {
int i=0;
string line;
while (getline(a,line)) {
cout << line << endl;
i++;
}
return i;
}
int hmwords(ifstream &a) {
int i=0;
char c;
a >> noskipws >> c;
while ((c=a.get()) && (c!=EOF)){
if (c==' ') {
i++;
}
}
return i;
}
int main()
{
int l=0;
int w=0;
string filename;
ifstream matos;
start:
cout << "Input filename- ";
cin >> filename;
matos.open(filename.c_str());
if (matos.fail()) {
goto start;
}
matos.seekg(0, ios::beg);
w = hmwords(matos);
cout << w;
/*c = hmchars(matos);*/
int RawData[w];
int n;
// Loop through the input file
while ( !matos.eof() )
{
matos>> n;
for(int i = 0; i <= w; i++)
{
RawData[n];
cout<< RawData[n];
}
}
//2nd Copied code ends here
int On = 0;
for(int j =0; j< w; j++) {
if(RawData[j] > 1000) {
On = On +1;
}
}
int OnArray [On];
int OffArray [w-On];
for(int j =0; j< w; j++) {
if(RawData[j]> 1000) {
OnArray[j] = RawData[j];
}
else {
OffArray[j] = RawData[j];
}
}
cout << "The # of lines are :" << l
<< ". The # of words are : " << w
<< "Number of T on elements is" << On;
matos.close();
}
But if it would be easier, i'm open to starting the whole thing again, as I don't understand exactly what all the copied code is doing. So to summarise, what I need is it to-
Ask for a filepath in the console
Open the file, and store each number (separated by a space) as an element in a 1D array
I can manage the actual operations myself I think, if I could just get it to read the file the way I need.
Thanks very much
Using C++11 and the Standard Library makes your task fairly simple. This uses Standard Library containers, algorithms, and one simple lambda function.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::string filename;
std::cout << "Input filename- ";
std::cin >> filename;
std::ifstream infile(filename);
if (!infile)
{
std::cerr << "can't open " << filename << '\n';
return 1;
}
std::istream_iterator<int> input(infile), eof; // stream iterators
std::vector<int> onvec, offvec; // standard containers
std::partition_copy(
input, eof, // source (begin, end]
back_inserter(onvec), // first destination
back_inserter(offvec), // second destination
[](int n){ return n > 1000; } // true == dest1, false == dest2
);
// the data is now in the two containers
return 0;
}
Just switch the type of variable fed to your fistream, created from new std:ifstream("path to file") into a int and c++ will do the work for you
#include <fstream> //input/output filestream
#include <iostream>//input/output (for console)
void LoadFile(const char* file)
{
int less[100]; //stores integers less than 1000(max 100)
int more[100]; //stores integers more than 1000(max 100)
int numless = 0;//initialization not automatic in c++
int nummore = 0; //these store number of more/less numbers
std::ifstream File(file); //loads file
while(!file.eof()) //while not reached end of file
{
int number; //first we load the number
File >> number; //load the number
if( number > 1000 )
{
more[nummore] = number;
nummore++;//increase counter
}
else
{
less[numless] = number;
numless++;//increase counter
}
}
std::cout << "number of numbers less:" << numless << std::endl; //inform user about
std::cout << "number of numbers more:" << nummore << std::endl; //how much found...
}
This should give you an idea how should it look like(you shoudnt use static-sized arrays tough) If you got any probs, comment back
Also, please try to make nice readable code, and use tabs/ 4 spaces.
even though its pure C, this might give you some hints.
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#define MAX_LINE_CHARS 1024
void read_numbers_from_file(const char* file_path)
{
//holder for the characters in the line
char contents[MAX_LINE_CHARS];
int size_contents = 0;
FILE *fp = fopen(file_path, "r");
char c;
//reads the file
while(!feof(fp))
{
c = fgetc(fp);
contents[size_contents] = c;
size_contents++;
}
char *token;
token = strtok(contents, " ");
//cycles through every number
while(token != NULL)
{
int number_to_add = atoi(token);
//handle your number!
printf("%d \n", number_to_add);
token = strtok(NULL, " ");
}
fclose(fp);
}
int main()
{
read_numbers_from_file("path_to_file");
return 0;
}
reads a file with numbers separated by white space and prints them.
Hope it helps.
Cheers

Detect newline byte from filestream

I'm trying to collect information from a textfile which contains names of organisations (without spaces) and floating integers. I want to store this information in an array structure.
The problem I'm having so far is collecting the information. Here is a sample of the textfile:
CBA 12.3 4.5 7.5 2.9 4.1
TLS 3.9 1 8.6 12.8 4.9
I can have up to 128 different numbers for each organisation, and up to 200 organisations in the textfile.
This is what my structure looks like so far:
struct callCentre
{
char name[256];
float data[20];
};
My main:
int main()
{
callCentre aCentre[10];
getdata(aCentre);
calcdata(aCentre);
printdata(aCentre);
return 0;
}
And the getdata function:
void getdata(callCentre aCentre[])
{
ifstream ins;
char dataset[20];
cout << "Enter the name of the data file: ";
cin >> dataset;
ins.open(dataset);
if(ins.good())
{
while(ins.good())
{
ins >> aCentre[c].name;
for(int i = 0; i < MAX; i++)
{
ins >> aCentre[c].data[i];
if(ins == '\n')
break;
}
c++;
}
}
else
{
cout << "Data files couldnt be found." << endl;
}
ins.close();
}
What I'm trying to achieve in my getdata function is this: store the organisation name first into the structure, then read each float into the data array until the program detects a newline byte. However, so far my check for the newline byte isn't working.
Assume that variables c and MAX are already defined.
How should I go about this properly?
The >> operator treats whitespace as a delimiter, and that includes newlines, so it just eats those and you never see them.
You need to read lines and then chop the lines up. The following bit of hackery illustrates the basic idea:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string line;
while( getline( cin, line ) ) {
istringstream is( line );
string cs;
is >> cs;
double vals[10];
int i = 0;
while( is >> vals[i] ) {
i++;
}
cout << "CS: " << cs;
for ( int j = 0; j < i; j++ ) {
cout << " " << vals[j];
}
cout << endl;
}
}
char byte = ins.peek();
Or
if(ins.peek() == '\n') break;
(Edit): You'll want to also check for an eof after your peek(), because some files may not have a ending newline.
I'd like to point out that you might want to consider using a vector<callCentre> instead of a static array. If your input file length exceeds the capacity of the array, you'll walk all over the stack.
I would read the file, one line after another and parse each line individually for the values:
std::string line;
while (std::getline(ins, line)) {
std::istringstream sline(line);
sline >> aCentre[c].name;
int i = 0;
while (sline >> aCentre[c].data[i])
i++;
c++;
}