including a text file

how to include a text.txt file from your computer

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1251 Views - Last Post: 28 August 2009 - 12:42 AM Rate Topic: -----

#1 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

including a text file

Post icon  Posted 27 August 2009 - 09:49 PM

Hi I'm trying to write a program using sequential files, it is supposed to open the file, count the number of lines, number, words, characters and punction marks. I don't know how to include my test.txt file in the program. The file I want to access is on my hard drive. The path is c:\Users\Documents\myname\test.txt

Can you also look at my counting the character code? I'm not sure if I did it correctly.

Thanks Again!!!

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <map>


using namespace std;



string getInputFileName();
int numChars(ifstream &in, int &numLines);
int numWords(ifstream &in, int &numLines);
int numPunct(ifstream &in, int &numLines);


void main()
{
	//lineBuffer = stream;
		int nLines,	//number of lines
			nWords,	//number of words
			nChars,	//number of character					
			nPunct;	//number of punction marks

		char c;
		
	

	
	ifstream InFile;
	InFile.open("C:/Users/Documents/est.txt", ios::in);
	string LineBuffer;
	
	
	string filename;

		//ifstream  ("c:/Users/Documents/text.txt", ios::in);
	
	
	
	filename = getInputFileName();
	InFile.open(filename.c_str());//open the file

	if (!InFile.is_open())
	{
		cerr << "File Error: Failed Could Not open file" << endl;	//cerr is a standard :: cout error message
		exit(0);
	}

	nChars = numChars (InFile, nLines);	//this will calculate the total number of lines
	nWords = numWords (InFile, nWords);	//this will calculate the total number of words
	nPunct = numPunct (InFile, nPunct);	//this will calculate the total number of punction marks
	
	cout << "The number of lines in the document are:  " << nLines << endl;
	cout << "The number of words in the document are:  " << nWords << endl;
	cout << "The number of punction marks in the document are: " << nPunct << endl;
	
	cin >> c;
	InFile.close(); //close and flush the file
}//end main



string getInputFileName()
{
	string fName;	
	cout << "Enter the name of the document you would like to retrieve(including the path): ";
	cin >> fName;

	return fName;

}


int numChars (ifstream &in, int &numLines)
{
	int numChars = 0;
	char ch; //character holder

	numLines = 0;

	
	
	while(in.get(ch))
	{
		if(ch != ' ')
		{
			if (ch != '\n')
				numChars++;
			else			
				numLines++; //increase the count of lines
			
		}
	}
	numChars += 1;
	return numChars;
}



int numWords(ifstream &in, int &inWords)
{
	in.clear();

	in.seekg(0,ios_base::beg);
	
	int numWords = 0;
	char ch;

	while (in.get(ch))	//get the character
	{
		if(ch == ' ' || ch == '\n' || ch == '\t')
			numWords++;
	}
	numWords +=1;
	return numWords;

}



int numPunct(ifstream &in, int &inWords)
{
	in.clear();
	in.seekg(0,ios_base::beg);
	
	char ch;
	 
	int numPunct = 0;
	
	//getline (InFile, lineBuffer);


	while (in.get(ch))
	{
		if(ch == '.' || ch == '?' || ch == '!' || ch == ';')
			numPunct++;
	}
	numPunct += 1;
	return numPunct;


}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: including a text file

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: including a text file

Posted 27 August 2009 - 09:52 PM

Try to put the test.txt file in the same folder as your .cpp file.You need to look over your code carefully. Some parts you have misspelled the file name.

This post has been edited by seeP+: 27 August 2009 - 10:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: including a text file

Posted 27 August 2009 - 10:03 PM

as a header file?
Was This Post Helpful? 0
  • +
  • -

#4 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: including a text file

Posted 27 August 2009 - 10:05 PM

No. Just copy and paste it to the same folder as your .cpp.
Was This Post Helpful? 0
  • +
  • -

#5 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: including a text file

Posted 27 August 2009 - 10:10 PM

I did that and it works, thanks. I don't know how to open my file now.
Was This Post Helpful? 0
  • +
  • -

#6 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: including a text file

Posted 27 August 2009 - 10:13 PM

View Postsp11765, on 27 Aug, 2009 - 09:10 PM, said:

I did that and it works, thanks. I don't know how to open my file now.

You mean in code?
Have you gone over your code and corrected the file names that have been misspelled?
Was This Post Helpful? 0
  • +
  • -

#7 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: including a text file

Posted 27 August 2009 - 10:18 PM

I think so

I have test_1.txt in a resource file

void main()
{
	//lineBuffer = stream;
		int nLines,	//number of lines
			nWords,	//number of words
			nChars,	//number of character					
			nPunct;	//number of punction marks

		char c;
		
	

	
	ifstream InFile;
	InFile.open("text_1.txt", ios::in);
	string LineBuffer;
	
	
	string filename;

	//ifstream Infile;
	//ifstream  ("c:/Users/Documents/text.txt", ios::in);
	
	
	
	filename = getInputFileName();
	InFile.open(filename.c_str());//open the file

	if (!InFile.is_open())
	{
		cerr << "File Error: Failed Could Not open file" << endl;	//cerr is a standard :: cout error message
		exit(0);
	}

	nChars = numChars (InFile, nLines);	//this will calculate the total number of lines
	nWords = numWords (InFile, nWords);	//this will calculate the total number of words
	nPunct = numPunct (InFile, nPunct);	//this will calculate the total number of punction marks
	
	cout << "The number of lines in the document are:  " << nLines << endl;
	cout << "The number of words in the document are:  " << nWords << endl;
	cout << "The number of punction marks in the document are: " << nPunct << endl;
	
	cin >> c;
	InFile.close(); //close and flush the file
}//end main



string getInputFileName()
{
	string fName;	
	cout << "Enter the name of the document you would like to retrieve(including the path): ";
	cin >> fName;

	return fName;



** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#8 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: including a text file

Posted 27 August 2009 - 10:24 PM

Quote

InFile.open("text_1.txt", ios::in);
string LineBuffer;


string filename;

//ifstream Infile;
//ifstream ("c:/Users/Documents/text.txt", ios::in);


You have it as text_1 not test_1
Was This Post Helpful? 0
  • +
  • -

#9 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: including a text file

Posted 27 August 2009 - 10:33 PM

when the screen pops up and asks me to input the filename, I'm typing test_1.txt.



[code]

string getInputFileName()
{
string fName;
cout << "Enter the name of the document you would like to retrieve(including the path): ";
cin >> fName;

return fname;

}


[/]

do I need this function?
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: including a text file

Posted 27 August 2009 - 10:34 PM

another voidmainer!!
but why cant you open the file?
You can do this a few different ways,
You can use the ofstream method, which is said to be a little
bit slower then using FILE but were not dealing with HUGE MASSIVE files im guessing.

ifstream out<<"Text_1.txt");
string line = ""; // buffers lines from text into this string
int count = 0; // gets number of lines
while(getline(out,line))
{
   count++; //count lines
}



the rest is pretty simles, you can also count characters by using a char, and then search the strings for punctiation marks
Was This Post Helpful? 0
  • +
  • -

#11 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: including a text file

Posted 27 August 2009 - 10:41 PM

Quote

when the screen pops up and asks me to input the filename, I'm typing test_1.txt

You input the file when you code inFile.open("file.txt"). So why do you need the program to ask you to input the file?

This post has been edited by seeP+: 27 August 2009 - 10:42 PM

Was This Post Helpful? 0
  • +
  • -

#12 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: including a text file

Posted 27 August 2009 - 10:54 PM

Thanks!
Was This Post Helpful? 0
  • +
  • -

#13 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: including a text file

Posted 28 August 2009 - 12:22 AM

I thought I would write a simple program, but my file still will not open. Will you check my code also?


[code]


#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{
string lineBuffer;
ifstream InFile;//("hello.txt");
ifstream out("hello.txt");

string line = " ";
char ch;

if(InFile.is_open())
{
int numChars =1,
numLines =1,
numWords =1,
numPunct =1;
while(!InFile.eof()) //while not the end of the file
{
while(getline(out,line))
{
getline (InFile, lineBuffer);
if(ch != '\n'){
numChars++;
cout << "The number of characters in the document are: " << numChars << endl;
}else
numLines++;
cout << "The number of lines in the document are: " << numLines << endl;


if (ch == ' ' || ch == '\n' || ch == '\t'){
numWords++;
cout << "The number of words in the document are: " << numWords << endl;
}
if (ch == '!' || ch == '.' || ch == '?' || ch == ';'){
numPunct++;
cout << "The number of punction marks in the document are: " << numPunct << endl;
}


}

}
InFile.close();


}else cerr << "Open document failed" << endl << endl;
exit (0);








return 0;



}




[/]
Was This Post Helpful? 0
  • +
  • -

#14 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: including a text file

Posted 28 August 2009 - 12:29 AM

could you post the errors that you are getting and also please close the code tags like this
[/code]

Thank you

And here is this on propose?

ifstream InFile;//("hello.txt");


im guessing

InFile.open("hello.txt", ios::in);


im not even sure about what i just wrote, G-night
Was This Post Helpful? 0
  • +
  • -

#15 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: including a text file

Posted 28 August 2009 - 12:29 AM

I think I know where I made some mistakes, but I'm still not able to open my file. The object is called "out", not InFile".
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2