So I am reading in a file of zeros and ones.
1,1,1,0,0,0,0,0
1,1,1,0,1,1,0,0
1,1,1,0,1,1,0,0
1,1,1,0,0,0,1,0
1,1,1,0,0,0,1,0
1,1,1,0,0,0,1,0
1,1,1,0,0,1,1,0
1,1,1,0,0,0,0,0
I read in this file and store it into a 2d vector. I then print this 2D vector and get these results as output.
\311[\240\3778\311[\240\377P\311[\240\377h\311[\240\377\200\311[\240\377\230\311[\240\377\260\311[\240\377\310\311[\240\377\340\311[\240\377\370\311[\240\377\312[\240\377(\312[\240\377#\312[\240\377X\312[\240\377p\312[\240\377\210\312[\240\377\240\312[\240\377\270\312[\240\377\320\312[\240\377\350\312[\240\377\313[\240\377\313[\240\3770\313[\240\377H\313[\240\377`\313[\240\377x\313[\240\377\220\313[\240\377\250\313[\240\377\300\313[\240\377\330\313[\240\377\360\313[\240\377\314[\240\377 \314[\240\3778\314[\240\377P\314[\240\377h\314[\240\3771110+2+,0,+a_0co0%We0Enab1AK1ages1eKey0r_1ons_1rvic0_000069631Ap1SUse1co0PKSe1NSLi1e_0"NSA0led_1tion1NSPr1DMSE0ging0mptD0ionE1sDat0appl1A_001D6961y_0mati0$NSA0able1nEna0sRequested_Microsoft Controller_#NSAutomaticQuoteSubstitutionEnabled\Dell S2830dn_AppleMiniaturizeOnDoubleClickQ1 \2410Uen-US3A\300\274\265\302\317+w\32134VdirMap\320 U10.14\241:\323;<=>?#WreplaceTwithRonSomwZOn my way!"?\2003A\300\321i\245z% XMaximizeUen_US\321GH_NSWebServicesProviderW,\200\306IJKL_qDisplayName_NSProviderIdentifierVGoogle^com.google.www 3A\300\321j
\273\375 3A\300\320Sa.$p\245RSTUV_0~/Desktop/CompSci 2100/2100Project3/2100Project3_ ~/Desktop/CompSci 2100/Project_2_~/Desktop/CompSci 2100_#~/Desktop/CompSci 2110/2210project2_0~/Desktop/CompSci 2110/2210Project3/2210Project3#?\340TDark \244]E^_RenRtrRfr _{\233\303\333\361 =_s\221\300\333\3709Y}\232\277\322\355\371*Hc\206\241\265\3223Z\201\244\306\335023457=FIPQRTZ\^emruy\204\206\213\224\225\236\244\247\310\315\344\373%+^\201\232\300\363\374 binput1.txt
\377\377\377\377\377q\205<оa\310\214 CPX#\360dCFPreferencesMessages\340#
\360\200CFPreferencesShmemName\220apple.cfprefs.501v1CFPreferencesShmemIndex0\364CFPreferencesShmemState#\360\200CFPreferencesShmemName\220apple.cfprefs.501v1CFPreferencesShmemIndex0\245CFPreferencesShmemState#\360\200CFPreferencesShmemName\220apple.cfprefs.501v1CFPreferencesShmemIndex0\343CFPreferencesShmemState#\360\200CFPreferencesShmemName\220apple.cfprefs.501v1CFPreferencesShmemIndex0YCFPreferencesShmemState#\360TCFPreferencesShmemName\220apple.cfprefs.501v1CFPreferencesPropertyList\200\255bplist00\337
234_com.apple.trackpad.version_.com.apple.trackpad.fourFingerHorizSwipeGesture_.com.apple.trackpad.fourFingerPinchSwipeGesture_.com.apple.trackpad.fiveFingerPinchSwipeGesture_com.apple.mouse.tapBehavior_ com.apple.trackpad.rotateGesture_com.apple.ColorSync.Devices_'com.apple.trackpad.enableSecondaryClick_com.apple.trackpad.pinchGesture_!com.apple.trackpad.scrollBehavior_5com.apple.trackpad.twoFingerFromRightEdgeSwipeGesture_)com.apple.trackpad.threeFingerDragGesture_(com.apple.trackpad.threeFingerTapGesture_/com.apple.trackpad.threeFingerHorizSwipeGesture_-com.apple.trackpad.fourFingerVertSwipeGesture_.com.apple.trackpad.threeFingerVertSwipeGesture_#PKSecureElementAvailableFlagsByHost_!com.apple.trackpad.momentumScroll_,com.apple.trackpad.twoFingerDoubleTapGesture \322+_0Device.cmra.63306330-3362-6536-3839-633063343435_0Device.mntr.C07EE717-D5E0-4933-131D-70E3DC02779B\322!_DeviceDescriptions_FactoryProfiles\321 Uen_USViPhone\322"#$*Y555810816_DeviceDefaultProfileID\322%&')_DeviceModeDescriptions_DeviceProfileURL\321(WDefault_[/System/Library/Frameworks/ICADevices.framework/Versions/A/Resources/Camera RGB Profile.iccY555810816\321,-^CustomProfiles\321./Q1_:/System/Library/ColorSync/Profiles/Generic RGB
it is actually a lot more than this but I think that would be too much for a question. It prints stuff like this sometimes only one sentence long sometimes forever. I have never experienced something like this I am just trying to print a 2D vector I have done this 1,000's of times with no problems. I am using xCode and here is my code for the whole project.
//read in the file into a double vector
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <sstream>
using namespace std;
class OBJs{
public:
string Obj;
int numItemsInObj;
};
//vector to store all of the data for the grid and the row of the big vector
vector<OBJs> row;
vector<vector<OBJs>> grid;
//maybe create an object for the found parameters
OBJs tempObj;
//functions
void load(string fileName);
void printObjs();
int main(int argc, const char * argv[]) {
string file;
cout << "Please enter your filename here: ";
cin >> file;
load(file);
printObjs();
return 0;
}
void load(string fileName){
//read in a line and insert the line(string) into the second index of the vector
string line;
string zeroOrOne;
ifstream file;
file.open(fileName);
//basically a guard let function in swift
if(!file){
cout << "Could not open the file " << fileName << endl;
//exit(0);
}
// you have successfully entered the file specified
while(getline(file, line)){
//grabbing each line of the file into the var line
//delimit this line by , in getline
istringstream ss(line);
while(getline(ss, zeroOrOne, ',')){
//put it into the row vector.
tempObj.Obj = zeroOrOne;
tempObj.numItemsInObj = 0;
row.push_back(tempObj);
}
//put the row in the grid.
grid.push_back(row);
}
file.close();
}
void printObjs(){
for(int i =0; i < grid.size(); i++){
for(int j =0; j< row.size(); j++){
if(grid[i][j].Obj == ""){
//cout << "nothing in this element" << endl;
} else {
cout << grid[i][j].Obj;
}
}
cout << endl;
}
}
In printObjs() you are limiting the loop index j with row.rize():
for(int j =0; j< row.size(); j++)
but use it to index into grid with grid[i][j].Obj. There is no reason that the size of row should be equal to that of grid[i]. Therefore you probably are accessing out-of-bounds causing undefined behavior.
It should probably be for(int j =0; j< grid[i].size(); j++) instead.
You also shouldn't be using global variables if it is not necessary. Both tmpObj and row are only used in load. Therefore declare them there.
Similarly grid can be a local variable, passed around between the functions.
There are couple of problems in your code. Firstly:
while (getline(file, line)) {
istringstream ss(line);
while (getline(ss, zeroOrOne, ',')) {
tempObj.Obj = zeroOrOne;
tempObj.numItemsInObj = 0;
row.push_back(tempObj);
}
grid.push_back(row);
}
Notice what's happening. You read every number per single line, add it to row and then add row to grid. What happens when you read the second line? You read more numbers and add more numbers to row. Now your row contains numbers from both first and second lines. You want to clear the row after copying it to the next element in the grid, like so:
while (getline(file, line)) {
istringstream ss(line);
while (getline(ss, zeroOrOne, ',')) {
tempObj.Obj = zeroOrOne;
tempObj.numItemsInObj = 0;
row.push_back(tempObj);
}
grid.push_back(row);
row.clear();
// ^ notice the clear() call here
}
There is also another issue. Here:
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < row.size(); j++) {
if (grid[i][j].Obj == "") {
//cout << "nothing in this vector" << endl;
} else {
cout << grid[i][j].Obj;
}
}
cout << endl;
}
you don't want to check for row.size(). You want to operate on the grid and you want the length of the ith row of the grid. Simply change it to:
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[i].size(); j++) {
// notice the change here ^^^
if (grid[i][j].Obj == "") {
//cout << "nothing in this vector" << endl;
} else {
cout << grid[i][j].Obj;
}
}
cout << endl;
}
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
int main()
{
int length [48];
int us[48];
int russ[38];
ifstream infile;
infile.open("data.txt");
if(infile.fail())
{
cout << "error" << endl;
return 1;
}
for(int i=0;i<48;i++)
{
infile >> length[i];
infile >> us[i];
while(i<=38)
{
infile>> russ[i];
}
infile.close();
}
for (int i = 0; i < 48; i++)
{
cout << length[i];
}
return 0;
}
I am trying to read each column above from a text file into a corresponding array. First column is length, second is us, third is russ. When i try to do a sample output to test it nothing is coming out. The program is compiling completely without bugs or errors but it is just not displaying the output.
Your problem is here
while(i<=38)
{
infile>> russ[i];
}
simply replace it with this:
while(i<38)
{
infile>> russ[i++];
}
Also, I don't know what you're trying to do exactly, since you're producing your output after too many for loops, therefore you're losing your data.
I'm working with visual studio 2013. I want to output a vector of objects into several files. I am able to create the output file if I just print everything to a single file, but if I try to output to multiple files, nothing happens.
#include<vector>
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
struct object
{
int a, b;
};
int main()
{
vector<object> DATA;
//fill DATA
ofstream out; string outname;
outname = "TL" + ".txt";
out.open(outname.c_str());
for (int i = 0; i < p; i++)
{
for (int k = 0; k < DATA.size(); k++)
{
out << i << endl;
if (DATA[k].a == i)
out << DATA[k].b << endl;
}
out << endl;
}
out.close();
return 0;
}
The above works exactly as I expect. However, if I rearrange it so that I could make separate files:
for (int i = 0; i < p; i++)
{
ofstream out; string outname;
outname = "TLR" + to_string(i) + ".txt";
out.open(outname.c_str());
for (int k = 0; k < DATA.size(); k++)
{
if (DATA[k].a == i)
out << DATA[k].b << endl;
}
out.close();
}
I get no output. I already checked to see if the files were being created in another directory and nada. Placing "cout << out.is_open()" after each of the cases shows that the single file is actually being opened (output 1), while the multiple files are not being opened (output 0).
Could anyone tell me what's going on and what can I do to fix this? I don't want to have to run the program and then open the output file to parse after I've made it.
Thank you.
When I was making the multiple files, I used the pipe, "|", (not shown) in the filename--which is a forbidden character in Windows' filenames.
I'm doing a lab in university that multiplies out 2 matrices from 2 different files and in one of the questions i've been asked to print the results on the console (which is not a problem, it's already done) but printing it to a new save file is the problem.
i'm fairly new to c++ so i apologize in advance if the mistake is obvious :(
here is my code so far....
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
///////// my output file /////////////
string outfile1, save;
cout<<"Please enter the name of the file you want to save the results in"<<endl;
cin>>outfile1;
ofstream outfile(outfile1.c_str());
if(!outfile)
{
cout<<"Please drag in the right file you want the results to be saved on"<<endl;
system("PAUSE");
return -1;
}
/////// sitting up the first matrix file ////////
string matrixA;
cout<< "Please drag file (matrix1) into this window"<<endl;
cin>>matrixA;
ifstream infile;
infile.open(matrixA.c_str());
if(!infile)
{
cout<<"ERROR: Wrong file, please try restart program and try again."<<endl;
return -1;
}
string matrix1;
int a[5][5];
for (int i = 0; i<5;i++)
{
for(int j = 0;j<5;j++)
{
getline(infile,matrix1,',');
a[i][j]= stoi(matrix1);
cout<< a[i][j]<<" ";
}
cout<<endl;
}
infile.close();
/////// sitting up the 2nd matrix file ////////
string matrixB;
cout<< "Please drag file (matrix2) into this window"<<endl;
cin>>matrixB;
ifstream infile2;
infile2.open(matrixB.c_str());
if(!infile2)
{
cout<<"ERROR: Wrong file, please try restart program and try again."<<endl;
return -1;
}
string matrix2;
int b[5][5];
for (int k = 0; k<5;k++)
{
for(int l = 0;l<5;l++)
{
getline(infile2,matrix2,',');
b[k][l]= stoi(matrix2);
cout<< b[k][l]<<" ";
}
cout<<endl;
}
infile2.close();
////////////// CALCULATIONS //////////////////////
cout<<"The product of both matrices is: "<<endl;
int result[5][5];
while (outfile1)
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
result[x][y] = 0;
for(int z = 0; z < 5; z++)
{
result[x][y] += a[x][z]*b[z][y];
}
cout << result[x][y] <<" ";
outfile1<< result[x][y]<<" "; // <<---------- why can i not do this?
}
cout<<endl;
}
system("PAUSE");
return 0;
}
is there an easier way to do it? if i try to run the code as it is, it gives me a highlight under the first "<<" saying (no operator "<<" matches these operands)
outfile1<<result[x][y]<<" ";
Your program doesn't compile because of two reasons.
while (outfile1)
outfile1 is an std::string (the output file name) and is not convertible to bool. It's not quite clear what you tried to achieve with this code.
outfile1<< result[x][y]<<" ";
Again, outfile1 is an std::string. Your output stream is outfile, so you should change this to
outfile << result[x][y] << " ";
outfile1 is of type string, while outfile is of type ofstream. Change outfile1 to be outfile on lines 86 and 100 and it should compile as expected.
You may want to name your variables differently to help prevent errors like this, perhaps naming outfile1 as outfileName.
I ve got a txt file with double matrix 50x8. The first two lines contains the array size
50
8
the 50x8 matrix. When i trid to read this file with the above code:
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ifstream infile;
infile.open("C:/Users/zenitis/Desktop/BTHAI_2.3b-src/BTHAI/txtFiles/W1.txt");
double events[50][8];
while (!infile.eof())
{
for(int j=0;j<50;j++)
{
for(int k=0; k<8;k++)
{
infile >> events[j][k];
// infile.get(c
}
}
} //end while
infile.close();
for(int i = 0; i<50; i++){
for(int l=0; l<8; l++){
cout << events[i][l] << " ";
}
cout << "\n";
}
cout << events[0][0];
system("pause");
return 0;
}
Firstly when i print the results the first two elements of the events matrix are the last two of the file. Secondly any idea how to read just the two first elements which is in fact the size of the matrix????
You read the number of rows and columns like this:
int R, C;
infile >> R;
infile >> C;
You do it before the nested loops that read the rest of the file. Then you use the numbers from the file as your end-of-loop targets, rather than hard-coding 50 and 8.