Infinite While Loop and Getting Incorrect Output - c++

I am currently trying to build a program that reads in a file, scans through that file, and outputs all words in that file surrounded by " marks. I am currently stumped and was hoping to get some help!
#include <iostream>
// For file I/O:
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
// Prototype the count function so we can have it below it's first
// use in main().
void count(istream& in, int& lines, int& words, int& characters);
/*
* wc <filename>
*/
int main(int argc, char *argv[])
{
if (argc < 2) {
cerr << "Usage: wc <filename>" << endl;
return 0;
}
// Open the file specified by argv[1] for reading:
// Constructs a ifstream object called "in":
ifstream in(argv[1]);
// Was there a problem opening the file?
if (!in.good()) {
cerr << "Unable to open file [" << argv[1] << "] for reading." << endl;
return 1;
}
int lines = 0, words = 0, characters = 0;
count(in, lines, words, characters);
cout << setw(5) << lines << " " << words << " " <<
characters << " " << argv[1] << endl;
// Close the input stream:
in.close();
}
void count(istream& in, int& lines, int& words, int& characters)
{
int i;
char s;
int ch;
bool inword = false;
// Read until the end of file is reached, or there was an error:
while (!in.eof()) {
// Read a character from the input stream "in":
s = in.get(); //Set char s = in.get
for(i=0; s != 0; i++){ //Loop to iterate through the characters
while(s == '"'){ //While s is equal "
cout << s << endl; // Print s
if(s == '"') // If we hit another ", then we break
break;
}
}
if (in.good() == false) return;
characters++;
if (!isspace(ch) && !inword) {
inword = true;
words++;
} else if (isspace(ch) && inword) {
inword = false;
}
if (ch == '\n') lines++;
}
}

your algorithm seems wrong.. in the for loop you compare to 's' but you're not updating it... try something like this in your main loop(QnD):
while (!in.eof() && (s = in.get()) != '"'); // read to first quote char
/* this is the word we want.. run to end quote marks.. */
while (!in.eof() && (s = in.get()) != '"') {
cout << s;
}
cout << endl;

Related

C++ How to Check what words aren't in 2 similar files

i was trying to find a way to check two different files and get, from the second, all lines that aren't in the first.. but does all the opposite.
I tried the possible to solve this but nothing...
This is the code:
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "");
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
stringstream buffer;
buffer << "C:\\Users\\" << username << "\\Desktop\\";
stringstream buffer2;
buffer2 << "C:\\Users\\" << username << "\\Desktop\\Legit.txt";
stringstream buffer3;
buffer3 << "C:\\Users\\" << username << "\\Desktop\\Unlegit.txt";
stringstream buffer4;
buffer4 << "C:\\Users\\" << username << "\\Desktop\\result.txt";
string results = buffer4.str();
int offset;
int num;
num = 1;
string search;
string linea;
string legit;
string unlegit;
string line;
cout << "Is the Legit.txt file at '" << buffer.str() << "'? [Y/N]: ";
cin >> legit;
if (legit == "Y" || legit == "y"){
}else if(legit == "N" || legit == "n"){
return 0;
}else{
cout << "\n.";
return 0;
}
string legitfile = buffer2.str();
cout << "\nIs the Unlegit.txt file at '" << buffer.str() << "'? [Y/N]: ";
cin >> unlegit;
if (unlegit == "Y" || unlegit == "y"){
}else if(unlegit == "N" || unlegit == "n"){
return 0;
}else{
cout << "\n";
return 0;
}
string unlegitfile = buffer3.str();
ifstream file(legitfile.c_str());
if(file.is_open()){
while(getline(file, line)){
ifstream MyFile(unlegitfile.c_str());
if(MyFile.is_open()){
while(!MyFile.eof()){
getline(MyFile,linea);
if((offset = linea.find(line, 0)) != string::npos) {
cout << "\n[" << num << "]" << " Word Found: " << line << "\n";
num++;
fstream result(results.c_str());
result << line << "\n";
result.close();
}
}
MyFile.close();
}
}
file.close();
return 0;
}else{
cout << "\nThe file '" << legitfile << "' does not exist.";
cout << "\nThe file '" << unlegitfile << "' does not exist.";
}
}
As i said, This code checks which words are equals in both (first & second) files and, once found, writes them to a third file, there is a way to do the opposite (check the two files and get the words that aren't equals)? Thank you so much!
I'm new, both in the forum and in C++, sorry if I make any mistakes. (sorry for my bad english too).
The classic solution to this sort of problem is to use a hash table collection to represent all the words in the first file. Then while iterating items from the second file, consult the set constructed of the first file. In C++, the std::unordered_set will do fine.
#include <unordered_set>
using namespace std;
unordered_set<string> firstFileSet;
unordered_set<string> missingFromSecondFileSet;
string line;
while(!firstfile.eof())
{
getline(firstfile,line);
firstFileSet.insert(line);
}
Then for each word in the second file, use a second set collection to keep track of what words are missing.
while(!secondfile.eof())
{
getline(secondfile,line);
if (firstFileSet.find(line) != firstFileSet.end())
{
missingFromSecondFileSet.insert(line);
}
else
{
firstFileSet.erase(line);
}
}
After the above runs, firstFileSet contains all the lines in the first file that were not present in the second. missingFromSecondFileSet contains all the lines in the second file that were not in the first:
for (auto &s : firstFileSet)
{
cout << s << " was in the first file, but not the second" << endl;
}
for (auto &s : missingFromSecondFileSet)
{
cout << s << " was in the second file, but not the first" << endl;
}
There is a program called diff on linux which does just what you are looking to do in C++.
It is written in C so you can just copy its source code =P
for (;; cmp->file[0].buffered = cmp->file[1].buffered = 0)
{
/* Read a buffer's worth from both files. */
for (f = 0; f < 2; f++)
if (0 <= cmp->file[f].desc)
file_block_read (&cmp->file[f],
buffer_size - cmp->file[f].buffered);
/* If the buffers differ, the files differ. */
if (cmp->file[0].buffered != cmp->file[1].buffered
|| memcmp (cmp->file[0].buffer,
cmp->file[1].buffer,
cmp->file[0].buffered))
{
changes = 1;
break;
}
/* If we reach end of file, the files are the same. */
if (cmp->file[0].buffered != buffer_size)
{
changes = 0;
break;
}
}
Taken from ftp://mirrors.kernel.org/gnu/diffutils/diffutils-3.0.tar.gz > src/analyze.c

The first character in the input file is the last character in the output file and vice-versa

I want to print out the first character in the input file is the last character in the output file and vice-versa. But I stuck at how to print out the output.
I need to use arrays. I will read from input file into a character array and the write from the array to the output file.
Example:
Input.txt: A B C D E H
output.txt: H B C D E A
This is my code
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string FileName, FileName2;
string s, temp, FirstChar, LastChar;;
char again = 'Y';
bool close = false;
char MAXSIZE[1024];
while (close == false)
{
cout << "Open the file: ";
cin >> FileName;
ifstream ReadFromFile(FileName);
if (ReadFromFile.is_open())
{
cout << "Succeed to open the file!\n";
// Read character from the input to array
while (!ReadFromFile.eof())
{
ReadFromFile >> MAXSIZE;
cout << MAXSIZE << " ";
}
cout << endl;
cout << "Enter the first character: ";
cin >> FirstChar;
cout << "Enter the last character: ";
cin >> LastChar;
swap(FirstChar, LastChar);
// I stuck at here
ifstream in(FileName);
cout << "Enter a name for a copy file: ";
cin >> FileName2;
ofstream out(FileName2);
while (getline(in, s))
out << s << "\n";
cout << "Close the program and then open your copy file.";
cout << endl << endl;
close = true;
}
else{
cout << "Failed to open the file!\n";
do {
cout << "Do you want to do it again(Y) or Close (N)? ";
cin >> again;
} while (again != 'y' && again != 'Y' && again != 'n' && again != 'N');
if (again == 'y' || again == 'Y')
close = false;
else
close = true;
cout << endl;
}
}
system("pause");
return 0;
}
Your task (according to your explanation) require:
1) reading from input file to array
2) changing the first and the last characters
3) saving array to output file
So, the first and the last characters should not be asked from standard input (keyboard).
#include <iostream>
#include <fstream>
using namespace std;
// filenames can be given as command line arguments
// change the code if you want to read them from standard input
int main(int argc, char* argv[])
{
ifstream inf;
ofstream outf;
size_t counter = 0;
const size_t MAXSIZE = 1024; // MAXSIZE - name of constant
char buffer[MAXSIZE]; // buffer - name of array
// check the command line arguments
// Alternatively you can define: string InpFileName, OutFileName;
// as it is in your code and enter values (cin >>) instead using argv
// if so, you should change inf.open(argv[1]); to inf.open(InpFileName);
// and outf.open(argv[2]); to outf.open(OutFileName);
if (argc != 3)
{
cerr << "Two arguments are required:" << endl
<< " 1) name of existing file (to read)" << endl
<< " 2) name of new file (to create)" << endl;
return 1;
}
// open files
inf.open(argv[1]);
outf.open(argv[2]);
// check files
if (!inf.is_open() || !outf.is_open())
{
cout << "ERROR: some trouble with files." << endl;
return 2; // stop the program
}
// process
// 1) reading
while (counter < MAXSIZE){
buffer[counter] = inf.get();
if (buffer[counter] != EOF)
{
counter++;
}
else
{
counter--; // the last character is not valid
break; // end of file
}
}
// 2) changing
char b = buffer[counter];
buffer[counter] = buffer[0];
buffer[0] = b;
// 3) output
for (int i = 0; i <= counter; i++)
{
outf.put(buffer[i]);
}
// close files
inf.close();
outf.close();
return 0;
}
UPDATE:
Clarify the task for cases when some unprintable character (like space) is the first or the last
Looks like a homework problem for a C++ class. One hint which may help is to
divide the file into blocks of X bytes and read the blocks in reverse order....
std::istream::seekg

fputc leaves the first line empty before inserting text

I use getchar() and loop to get a text and use fputc() to put into the text file, but it always leaves the first line empty in the text file after writing. The loop stops when character input is a dot (.). How can I delete the first line?
Updated (12/29/2016): I used DevC++ and the codes run finely without creating an empty blank, but my VisualStudio2015 had the problem.
Example: create a file named test.txt
input: this is a text.
output: (in the text file)
[ a blank line ]
this is a text
void writeFile(char *fileName){
FILE *fp;
fp = fopen(fileName, "wt"); //write text
if (fp == NULL) {
cout << "Failed to open" << endl;
fclose(fp);
}
else {
int i = 0;
char c = '\0';
cout << "Enter a text and end with dot (.): ";
fflush(stdin);
//c = getchar();
while (c != '.') {
fputc(c, fp);
c = getchar();
}
cout << "Written successfully" << endl;
fclose(fp);
}
}
Out of curiosity, is there a reason for the C functions? Doing something like this in C++ would be better suited to using streams, like:
#include <iostream>
#include <fstream>
using namespace std;
void writeFile(const char *fileName)
{
ofstream writeToFile;
writeToFile.open(fileName);
if (!writeToFile.is_open()) {
cout << "Failed to open" << endl;
return;
} else {
string stringToWrite{""};
char c = '\0';
cout << "Enter a text and end with dot (.): ";
while (c != '.') {
std::cin >> c;
stringToWrite += c;
}
writeToFile << stringToWrite << endl;
cout << "Written successfully" << endl;
writeToFile.close();
}
}
int main()
{
const char *fileName="test.txt";
writeFile(fileName);
return 0;
}
or, alternatively:
#include <iostream>
#include <fstream>
using namespace std;
void writeFile(const char *fileName)
{
ofstream writeToFile;
writeToFile.open(fileName);
if (!writeToFile.is_open()) {
cout << "Failed to open" << endl;
return;
} else {
string stringToWrite{""};
cout << "Enter text and press return: ";
getline(cin, stringToWrite);
writeToFile << stringToWrite << endl;
cout << "Written successfully" << endl;
writeToFile.close();
}
}
int main()
{
const char *fileName="test.txt";
writeFile(fileName);
return 0;
}
c is 0 on the first pass, hence the blank line.
change the while loop to
while( (c = getchar()) != EOF)
{
if(c == '.')
break;
}
It looks a bit odd, but is idiomatic for reading characters from a stream in C.

C++ string not concatenating char when int even though it is a char

Here is my runnable code:
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char **argv) {
if (argc > 1) { // passed in parameter
ifstream file(argv[1]); // create file from second parameter
string line;
if (file) { // file exists and has been opened
//stack *stmt = new stack();
while (getline(file, line)) { // run through lines of file
string word = "";
cout << "LINE: " << line << endl;
for (int i = 0; i < line.length(); i++) { // run through char of line
char ch = line[i];
if (ch == ' ') {
if (word == "") {
continue;
} else {
//stmt->push(word);
cout << "\x1b[34;1mWORD: " << word << "\x1b[0m" << end;
word = "";
}
} else {
word += ch;
}
}
}
cout << stmt->size() << endl;
while (stmt->has_next()) {
cout << stmt->pop() << endl;
}
} else {
cerr << "\x1b[31;1mNo such file, \x1b[21m" << argv[1] << "\x1b[0m" << endl;
}
} else { // no parameters passed in
cerr << "\x1b[31;1mYou did not specify any files to parse\x1b[0m" << endl;
}
return 0;
}
It seems straight forward. However, when the character, ch, is the character for an integer (like '0' to '9'), the character simply is not appended.
Does this happen with you guys (if you create a test file like so:
this is from line 1
line 2
3
and then after g++ test.cpp - if you name file test.cpp - and ./a.out test - if you store the above test file to test - you will see (frustratingly so) that WORD: ... (in blue) never contains a number if the number is a single character.
I.E. OUTPUT for me:
LINE: this is from line 1
WORD: this
WORD: is
WORD: from
WORD: line
LINE: line 2
WORD: line
LINE: 3
This is very frustrating; please help and explain why it is not working or comment that it works for you.
It seems that words are printed only if there is a space after them:
if (ch == ' ') {
....
cout << "\x1b[34;1mWORD: " << word << "\x1b[0m" << end;
....
}
This is not the case for the numbers in the given test.

Can't figure out how to print sequences of letters in C++

I am working on a project where I will be able to read in a file that contains any text, like the sample text below. Then, character by character, it will be able to output n-character long sequences (represented below as a read-in value given by the user along the lines of 1, 2, 3, 4...) along the whole length of the text. So, for example:
As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a gigantic insect.
If the user provided 2 as the sequence length, the program should spit out: "As" "s " " G" "Gr" "re" "eg" "go" "or" "r " and so on...
I have written this code but don't know why it won't work. Right now, it doesn't spit out every possible variation of the sequence. Any suggestions would be very helpful. Thanks.
#include "genlib.h"
#include <iostream>
#include <fstream>
#include "simpio.h"
#include "random.h"
#include "vector.h"
#include "map.h"
/* Private Instance Variables */
int seed_length;
string line;
string seed_string;
string next_string;
char ch;
/* Function Prototypes */
string promptUserForFile(ifstream & infile);
int main() {
ifstream infile;
promptUserForFile(infile);
// Ask what order of Markov model to use.
cout << "What order of Markov model should we use? ";
cin >> seed_length;
while (infile.eof() == false) {
ch = infile.get();
for (int i = 0; i < seed_length - 1; i++) {
cout << "ch up here is " << ch << endl;
if (isspace(ch) && i == 0) {
seed_string += ch;
} else {
seed_string += ch;
ch = infile.get();
}
}
next_string = ch;
if (isspace(ch)) {
next_string = " ";
} else {
char trythis = infile.get();
next_string += trythis;
}
cout << seed_string << endl;
cout << next_string << endl;
seed_string = "";
next_string = "";
}
cout << "TEST" << endl;
// Close the file when you're done storing all of the scores.
infile.close();
return 0;
}
string promptUserForFile(ifstream & infile) {
string prompt = "Please input your filename: ";
while(true) {
cout << prompt;
string filename;
getline (cin, filename);
infile.open(filename.c_str());
if(!infile.fail()) return filename;
infile.clear();
cout << "Unable to open that file. Try again." << endl;
if (prompt == "") prompt == "Input file: ";
}
return 0;
}
The code has two problems.
The special handling for isspace is broken:
if (isspace(ch) && i == 0) {
seed_string += ch;
} else {
seed_string += ch;
ch = infile.get();
}
This essentially means that if the first character in this loop is a space, it will be added twice.
Every character received from infile.get() is only added to seed_string once (with the exception of isspace characters).
A better way to code this is to recognize that:
You have to ignore consecutive isspace characters.
Every sequence can be obtained by removing the first character of the preceding sequnce and appending the next character from the file.
Here is a better implementation; it takes the order of the Markov model in the first command line parameter and takes the text from standard input. By encapsulating the skipping of duplicate spaces in a separate function, you don't have to deal with it in the main body of the algorithm.
#include <iostream>
#include <cstdlib>
char next_character() {
static bool was_space = false;
char ret = 0;
do {
ret = std::cin.get();
} while (was_space && std::isspace(ret));
if (std::isspace(ret)) {
was_space = true;
ret = ' ';
} else {
was_space = false;
}
return ret;
}
int main(int argc, char **argv) {
if (argc != 2) return 0;
int mlen = std::atoi(argv[1]);
std::string seq;
for (unsigned i = 0; i < mlen; ++i) {
seq += next_character();
}
std::cout << seq << '\n';
while (true) {
seq.erase(0, 1);
char c = next_character();
if (std::cin.eof()) break;
seq += c;
std::cout << seq << '\n';
}
return 0;
}
Example input:
This is a test
Example output:
This
his i
is is
s is
is a
is a
s a t
a te
a tes
test