Hi, I'm trying to read in a text file line by line, taking each line in as a string in C++. Unfortunately, when I run it in the terminal, the line wraps around and is cut into two. Even when I zoom out, the terminal still does this. Anyone know how to make it take in more lines???!!!
Also, I have a problem where when I cout the vector of strings where it is in bold, I get what I want, but when I create a for loop at the end of the program to cout it, it puts the last few lines at the beginning of the vector...
This is my code so far.
Code:
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
ifstream datafile;
char filename[80];
cout << "Enter file: ";
cin >> filename;
datafile.open(filename);
string line;
int j=0;
int q=0;
int numFA=0;
int nummiss=0;
int numhit=0;
int numtarget=0;
bool hit2ndlast = 0;
bool hitlast=0;
size_t targetloc;
size_t responseloc;
string str1= "Response" ;
string str2= "Target";
string str;
char charline[80];
vector<string> tokens;
vector<int>responseline;
if (datafile.is_open())
{
while (! datafile.eof())
{
getline (datafile, line);
vector<string> returned_strings;
char* current_token =strtok(const_cast<char*>(line.c_str()), " ");
while (current_token != NULL)
{
returned_strings.push_back(string(current_token));
tokens.push_back(string(current_token));
current_token = strtok(NULL, " ");
}
for (unsigned int i = 0; i < returned_strings.size(); ++i)
{
tokens[i] = returned_strings[i];
//cout << tokens[i]<<endl;
j++;
}
}
datafile.close();
}
for (int z=5; z<(j-2);)
{
str=tokens[z];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (targetloc != string::npos)
{
numtarget++;
z++;
str=tokens[z];
responseloc=str.find(str1);
if ((responseloc!=string::npos) && (z<(j-2)))
{
numhit++;
responseline.push_back(int(z));
responseline[q]=z;
q++;
z++;
str=tokens[z];
responseloc=str.find(str1);
if ((responseloc !=string :: npos)&& (z< (j-2)))
{
numFA++;
}
while ((responseloc != string::npos) && (z < (j-2)))
{
z++;
str=tokens[z];
responseloc=str.find(str1);
}
}
else if (z<(j-2))
nummiss++;
}
else if (responseloc !=string::npos)
{
cout << "FA line is " << tokens[z] << endl<<endl;
numFA++;
z++;
str=tokens[z];
responseloc=str.find(str1);
if (responseloc != string::npos)
{
while ((responseloc != string::npos) && (z < (j-2)))
{
z++;
str=tokens[z];
responseloc=str.find(str1);
}
}
}
else
{
z++;
}
}
str=tokens[j-3];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string::npos)//response/..../....
{
str=tokens[j-2];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string :: npos)//response/response...
{
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (targetloc !=string ::npos)
{
nummiss++;//response/response/target=miss
numtarget++;
}
//response/response/response=nada
//response/response/standard=nada
}
else if (targetloc !=string :: npos)//response/target...
{
numtarget++;
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string::npos)
{
numhit++; // response/target/response=1 hit
hitlast=1;
}
else if (targetloc !=string::npos)
{
nummiss= nummiss+2; //response/target/target=2 misses
numtarget++;
}
else nummiss++; //response/target/standard=1 miss
}
else//response/standard...
{
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string :: npos)
numFA++; //response/standard/response=1 FA
else if (targetloc !=string::npos)
{
nummiss++; //response/standard/target=1 miss
numtarget++;
}
//response/standard/standard=nada
}
}
else if (targetloc !=string :: npos) //target/..../....
{
//no target++ here because already counted in the previous iterations for j-3
str=tokens[j-2];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string :: npos)//target/response...
{
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (targetloc !=string ::npos)
{
nummiss++;//target/response/target=1 hit and 1 miss
numhit++;
numtarget++;
hit2ndlast=1;
}
else
{
numhit++;//target/response/response= 1 hit
hit2ndlast=1; //target/response/standard=1 hit
}
}
else if (targetloc !=string :: npos)//target/target...
{
numtarget++;
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string::npos)
{
numhit++; // target /target/response=1 hit and 1 miss
nummiss++;
hitlast=1;
}
else if (targetloc !=string::npos)
{
nummiss= nummiss+3; //target/target/target=3 misses
numtarget++;
}
else nummiss=nummiss+2; //target/target/standard=2 miss
}
else//target/standard...
{
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string :: npos)
{ numFA++; //target/standard/response=1 FA and 1 miss
nummiss++;
}
else if (targetloc !=string::npos)
{
nummiss=nummiss+2; //target/standard/target=2 misses
numtarget++;
}
else nummiss++;//target/standard/standard=1 miss
}
}
else //standard/..../...
{
str=tokens[j-2];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string :: npos)//standard/response...
{
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (targetloc !=string ::npos)
{
nummiss++;//standard/response/target=1 FA and 1 miss
numFA++;
numtarget++;
}
else numFA++;//standard/response/response= 1 FA
//standard/response/standard=1 FA
}
else if (targetloc !=string :: npos)//standard/target...
{
numtarget++;
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string::npos)
{
numhit++; // standard/target/response=1 hit
hitlast=1;
}
else if (targetloc !=string::npos)
{
nummiss= nummiss+2; //standard/target/target=2 misses
numtarget++;
}
else nummiss=nummiss+1; //standard/target/standard=1 miss
}
else//standard/standard...
{
str=tokens[j-1];
responseloc=str.find(str1);
targetloc=str.find(str2);
if (responseloc !=string :: npos)
{ numFA++; //standard/standard/response=1 FA
}
else if (targetloc !=string::npos)
{
nummiss=nummiss+1; //standard/standard/target=1 miss
numtarget++;
//standard/standard/standard=nada
}
}
}
if (hitlast==1)
{
responseline.push_back(int(j));
responseline[q]=j-1;
}
else if (hit2ndlast==1)
{
responseline.push_back(int(j));
responseline[q]=j-2;
}
cout << "Data for " << filename << endl << endl;
cout << "FA = " << numFA << endl;
cout << "hits = " << numhit<<endl;
cout << "misses = " << nummiss<< endl;
cout << "total number of targets was " << numtarget << endl << endl << endl;
for (unsigned int t=0; t<responseline.size(); t++)
{
int responseint= responseline[t];
//cout << tokens[responseint]<<endl;
strcpy(charline, const_cast<char*>(tokens[responseint].c_str()));
cout<<charline[27];
cout<<charline[28];
cout<<charline[29];
cout<<charline[30];
cout<<charline[31];
cout<<charline[32];
cout<<charline[33];
cout<<charline[34];
cout<<charline[34];
cout<<charline[36];
cout<<charline[37];
cout<<endl<<endl;
cout<<"beenie" <<endl<<endl;
}
cout << "second to last line a hit? " << hit2ndlast << endl;
cout << "last line a hit?" << hitlast << endl;
/*fstream myfile;
myfile.open ("summary.txt", ios::in | ios::out);
if (myfile.is_open())
{
cout << "The file opened. Yay!" << endl << endl;
myfile << "Data for " << filename << endl << endl;
myfile << "FA = " << numFA << endl;
myfile << "hits = " << numhit<<endl;
myfile << "misses = " << nummiss<< endl;
myfile << "total number of targets was " << numtarget << endl << endl << endl;
myfile.close();
}
else cout << "Unable to open file." << endl<<endl;
*/
return 0;
}