Hi. I am trying to read in test.cpp in the main function for analyzing. But I am not sure how to make use of getline(). Pls advise. Pls see [CODE].
[test.cpp]
using namespace std;
int main() {
//Checking braces & parentheses { int a = (7 + 2 + 3) * (7 - 3); }
} ___________________________________________
[CODE]
using namespace std;
const int MAX = 500;
struct Node; typedef Node* NodePtr; typedef void* VoidPtr;
struct Node { VoidPtr data; NodePtr next; };
class Stack
{
public:
Stack();
void push(VoidPtr);
VoidPtr pop();
bool isEmpty ();
private:
NodePtr ptr; char top; };
Stack::Stack() { ptr = 0; }
bool Stack::isEmpty () { return (ptr == NULL); }
void Stack::push (VoidPtr item)
{
NodePtr temp = new Node;
temp -> data = item;
if (ptr == NULL) { temp -> next = temp; ptr = temp; } else { temp -> next = ptr -> next; ptr -> next = temp; ptr = temp; } }
VoidPtr Stack::pop ()
{
try
{
if (ptr == NULL)
throw exception ();
NodePtr temp = ptr; VoidPtr item = ptr -> data; NodePtr leftNode = ptr -> next;
if (ptr -> next == ptr) ptr = NULL; else { NodePtr curr = ptr; while (curr -> next != ptr) curr = curr -> next;
curr -> next = leftNode; ptr = curr; }
delete temp; return item; } catch (exception e) { cout << "Stack empty: pop failed" << endl; return NULL; } }
class Symbol
{
public:
Symbol ();
Symbol (char, int);
void printSymbol () const; char getSymbol () const; void setSymbolInfo (char, int);
private: char ch; int lineNo;
};
int main()
{
Stack s;
char fileName [MAX];
cout << "Enter a file name to process: "; cin >> fileName; cin.getline(fileName, MAX);
ofstream outfile;
outfile.open ("outfile.txt"); // opens the file
outfile.close();
while (outfile)
{
if (outfile == false)
cout << "Error: file could not be opened" << endl;
outfile >> cin.getline(fileName, MAX);
}
outfile.close ();
return 0;
}
Hello Steve.
Nice to hear from you again.
It looks like you are trying to open a file for input, read it in line by line, and print it line by line to an output file.
You need to open the input file, and the output file, then read the inputfile with getline. You cannot read the input file with cin. Here is how you do it. I have removed all the code before main because it is not yet used. Make sure you read the code comments.
using namespace std;
int main() {
std::string fileName; cout << "Enter a file name to process: "; // Read you input into a string, not a char array, to prevent buffer overflow cin >> fileName;
// Open the input file. Check for succcess ifstream input(fileName.c_str()); if (!input) { cerr << "Cannot open " << fileName << endl; return 0; }
// Open the output file. Check for succcess ofstream outfile; outfile.open ("outfile.txt"); if (!outfile) { cerr << "Cannot open output file\n"; return 0; }
/* Read the input line by line. Notice that the eof is not set until you actually try to read PAST the end of file. In other words, reading the last line will not set the eof flag. We must read past the last line, and if the eof is set, don't write the output. */ do { std::string line; getline(input,line); if (input.eof()) break; outfile << line << endl; } while(true); return 0;
}
Advertisement