FILE I/O

Hi i'm new to this forum so please forgive me if i post this incorrectly.

i am writing a program as an assessment but i cant get my head around it.
what i need to do is have 2 files 1 whihc has a text document with words or a story type structure and in a second document words which replace other words within the first document.

etc.

Doc1 Story.


Hello i have a nice house in the woods, It is a lovely place to live.
However i live alone.

doc2_WordReplacer

hello:hi
a:in
live:stay



etc. what i am having issues with is storing the first doc into a string, when i wrote this the first time i had the entire text doc stored as char's but this wouldn't really help my issue. doc 2 would replace the words in doc1 based on left to right separated by a colon. would someone kindly explain the getline() and why it does not get the first line, it gets the second line in my tests (only tested with 2 lines). but it only gets the second line of the document. some suggestions would be kind.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  #include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;


void PerformReplacement(string proseFilename, string replacerFilename, string outputFilename);


int min(int a, int b)
{
	if (a < b)
		return a;

	return b;
}


int main()
{
	string proseFilename;
	string replacerFilename;
	string outputFilename;

	//Read file names from user input.
	cout << "Enter the name of your prose file:" << endl;
	cin >> proseFilename;
	cout << "Enter the name of your replacer file:" << endl;
	cin >> replacerFilename;
	cout << "Enter the desired name of your output file:" << endl;
	cin >> outputFilename;
	
	//Print names as they have been entered.
	cout << "Text file name set to '" << proseFilename << "', replacer file name set to '" << replacerFilename << "'." << endl;
	cout << "Output file name set to '" << outputFilename << "'." << endl;

	//Do the actual replacement.
	PerformReplacement(proseFilename, replacerFilename, outputFilename);
	
	cin.get();
	return 0;
}
//This function currently does nothing - that's what you have to change.
void PerformReplacement(string proseFilename, string replacerFilename, string outputFilename)
{
	
	//open a file
	ifstream fileIn;
	fileIn.open(proseFilename);

	//open replacer file
	ifstream replacer;
	replacer.open(replacerFilename);

	//open output file
	ofstream fileout;
	fileout.open(outputFilename);
	fileout << proseFilename;

	
	//read in some text
	string line;
	//getline(fileIn, line); //read in the first line
	while (fileIn.eof == false)
	{		
		getline(fileIn, line);		
	}
	cout << line << endl;
	//compare characters 
	//Vector is a list (Dynamic array)
	vector<string> wordList;
	string curWord;

	//use isalpha(line[i])
	for (int i = 0; i < line.size(); ++i)
	{
		if (isalpha(line[i]) && line[i] != ' '  && line[i] != ',' && line[i] != '\'')
		{
			//Add one character to the current word
			//line[i] is the character in slot i that we are currently looking at now.
			curWord = curWord + line[i];
		}

		else if (curWord.size() > 0)
		{
			//add the current word into our list of words
			//our *Vector* of words
			wordList.push_back(curWord);
			//empty the current word variable otherwise it will have the previous word in the variable still!
			curWord = "";
		}		
	}

	if (curWord != "")
	{
		wordList.push_back(curWord);
	}
	//print different order
	//print all words in order
	for (int i = 0; i < wordList.size(); ++i)
	{
		cout << wordList[i];		
		fileout << wordList[i] + " ";
	}
	
//Testing Code
/*
	char let;
	string word;
	vector <string> dict;

	fileIn >> word;
	cout << endl << word;

	cin.get();
*/
}

i seemed to have fixed part of this issue, but i still get one issue with this that i have not been able to figure out. it does not seem to detect newline characters, i had searched the net a bit but it seems that the getline() skips all whitespace and newline characters, i would like to confirm this and may ask how i can get around this. i had tried using

'\n' '\r' and char(012)

at the moment i'm just trying to get everything printed in CMD and can then transfer it later to a text file.

Thanks in advanced for the help.




//open a file
	ifstream fileIn;
	fileIn.open(proseFilename + ".txt");

	//open replacer file
	ifstream replacer;
	replacer.open(replacerFilename + ".txt");

	//open output file
	ofstream fileout;
	fileout.open(outputFilename + ".txt");
	//fileout << proseFilename;

	
	//Dictionary
	vector<string> wordList;
	vector <string> dictionary;
	string word, nword;
	int n = 0;
		

	//compare characters 
	//Vector is a list (Dynamic array)

	//read in some text
	string line = "";	
	cout << endl << endl << "		Document 1		" << endl << endl;


	while (!fileIn.eof())
	{
		getline(fileIn, line);
		//fileout << line << endl;
		//cout << line << endl;
		

		//store all seperate words into a new position in a vector

		for (int i = 0; i < line.length(); i++)
		{	

			if (line[i] != '\n' || line[i] != char('\n') || line[i] != char(012))
			{

			}
			else
			{
				cout << "ENTER" << endl;
			}


			if (isalpha(line[i]) || line[i] != ' ' || line[i] == '!' || line[i] == '?' || line[i] == '.' || line[i] == ',' || line[i] == '$' || line[i] == '%' || line[i] == '&' || line[i] == '"' || line[i] == ':')
			{
				word = word + line[i];
				//cout << word << endl;				
			}

			else if (word.size() > 0)
			{
				if (word == "how")
				{
					//cout << endl << "FOUND HOW" << endl;
					
					//word = "THISISASHAOO";
					wordList.push_back("THISISASHAOO");
					//wordList.push_back(word);
					word = "";
				}			

				else {

					cout << word;
					wordList.push_back(word);
					word = "";				
				}
			}
			
		}
	}
	
	for (int i = 0; i < wordList.size(); i++)
	{
		fileout << wordList[i] + " ";
	}
	

	fileIn.close();
	cout << "count: " << n;
	cout << "Read file completed!!" << endl;
	
	cin.get();
}
Do you realize that getline() extracts the delimiter from the stream and discards this character?

i had searched the net a bit but it seems that the getline() skips all whitespace and newline characters, i would like to confirm this


No getline() doesn't skip all whitespace characters. The purpose of getline() is to get a line of characters that may or may not contain whitespace characters. It stops processing the stream when it encounters a new line character. This newline character is extracted from the stream and discarded. Any other whitespace characters are extracted from the stream and placed into the stream.

Topic archived. No new replies allowed.