LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-08-2009, 03:26 PM   #1
Clarislock
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Rep: Reputation: 0
extending the num of char capacity in a line in terminal/vector question


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;
}

Last edited by Clarislock; 10-09-2009 at 12:09 PM.
 
Old 10-08-2009, 05:32 PM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
Put your code in code tags to make it readable.

You are using strtok() and specifying space as a delimiter. You didn't provide any sample lines, but that is a good reason for your lines to be split; you have specified that they'll be split on any space.

I didn't try to read the rest of your code to see how things are getting rearranged.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line gnome terminal question sudz28 Linux - Desktop 3 04-20-2012 03:21 AM
how many users / home capacity over command line laucian Linux - Newbie 4 02-20-2008 08:06 AM
Convert C++ String Vector to char array anamericanjoe Programming 1 12-12-2006 09:29 PM
question about extending partitions Lleb_KCir Linux - General 2 10-26-2004 08:25 PM
What's it called? Terminal? Concole? Command Line? It's a stupit question, I know. brynjarh Linux - Newbie 4 07-11-2004 02:09 AM


All times are GMT -5. The time now is 10:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration