|
-
September 23rd, 2009, 10:22 AM
#1
Converting txt file contents to char array
I've been at this for hours and I cant figure out why I can't convert the text in a txt file to a simple char array
Code:
// inFix to post fix converter
//Gary Chapman CMPS 401
//This does it correctly but, the User must type in the statement but they are given what to type.
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<vector>
#include<string>
#include <stdlib.h>
#include <stdio.h>
#include<stdlib.h>
#include<fstream>
const int size =50;
char postfix[size],stack[size];
int top=-1;
int precedence(char ch); // get the precedence of the operator
char pop(); // pop an element from the stack
char topelement(); // returns the top element of the stack
void push(char ch); // pushes an element into the stack
using namespace std;
int main()
{
string getdata;
int cont=0;
fstream data;
data.open("C:\\InFix.txt",ios::in);
while(!data.eof()){
data.getline(getdata,sizeof(data));
cout<<getdata<<endl;
}
system("pause");
ifstream inFile("C:\\InFix.txt");
ifstream ftext;
string word;
cout << getdata; //THis is EMPTY!
cout << "Please type The following statement" ;
cout << "\n" ;
cout << "\n";
cout << "THis will convert what is in InFix.txt to Post Fix";
cout << "\n";
char ele,elem,st[2];
int prep,pre,popped,j=0,chk=0;
strcpy(postfix," ");
char dirt[size] = "lol";
// char dirt
cout << dirt;
//Gets the inFIx typed but this is acutally getting written to the textfile not what you wrote.
//gets(infix);
//char fiaz[size] = inFile;
//char infix[size] << inFile;
char infix[size] = "X = A + B";
for(int i=0;infix[i]!=0;i++)
{
if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-')
postfix[j++]=infix[i];
else if(infix[i]=='(')
{
elem=infix[i];
push(elem);
}
else if(infix[i]==')')
{
while((popped=pop()) != '(')
postfix[j++]=popped;
}
else
{
elem=infix[i];
//stores precedence of operator from infix
pre=precedence(elem);
ele=topelement();
//stores precedence of operator at top stack
prep=precedence(ele);
if(pre > prep)
push(elem);
else
{
while(prep >= pre)
{
if(ele=='#')
break;
popped=pop();
ele=topelement();
postfix[j++]=popped;
prep=precedence(ele);
}
push(elem);
}
}
}
while((popped=pop())!='#')
postfix[j++]=popped;
postfix[j]='\0';
cout<<"\n post fix Version :"<<postfix<<endl;
//was able to write to a file but, only overwrote the exisiting file
inFile.close();
system("pause");
return 0;
}
int precedence(char ch)
{
switch(ch)
{
case '^' : return 5;
case '/' : return 4;
case '*' : return 4;
case '+' : return 3;
case '-' : return 3;
case '=' : return 2;
default : return 0;
}
}
char pop()
//pop the element from the stack
{
char ret;
if(top!=-1)
{ ret =stack[top];
top--;
return ret;
}
else
return '#';
}
char topelement()
// return top element from the stack without popping
{
char ch;
if(top!=-1)
ch=stack[top];
else
ch='#';
return ch;
}
void push(char ch)
// push an element in the stack
{
if(top!=size-1)
{
top++;
stack[top]= ch;
}
}
-
September 23rd, 2009, 11:28 AM
#2
Re: Converting txt file contents to char array
Code:
data.getline(getdata,sizeof(data));
Looks like a misuse of sizeof() to me. I don't know what you expect that to give you, but how much stack memory an fstream object takes up doesn't seem very relevant to the problem.
Why not just do it the easy way:
Code:
ifstream in("test.txt", ios::binary);
// get length of file
in.seekg (0, ios::end);
size_t length = is.tellg();
in.seekg (0, ios::beg);
// allocate memory
vector<char> buffer(length+1,0);
// read file
in.read (&buffer[0],length);
-
September 23rd, 2009, 11:31 AM
#3
Re: Converting txt file contents to char array
 Originally Posted by Lindley
Why not just do it the easy way:
Code:
ifstream in("test.txt", ios::binary);
// get length of file
in.seekg (0, ios::end);
size_t length = is.tellg();
in.seekg (0, ios::beg);
// allocate memory
vector<char> buffer(length+1,0);
// read file
in.read (&buffer[0],length);
A little easier, IMO:
Code:
std::ifstream in("...");
typedef std::istreambuf_iterator<char> in_t;
std::string buffer((in_t(in)), in_t());
EDIT: It's probably also worthwhile to mention that this method (verbatim) will probably be [much] slower than what has been proposed above.
That said, I doubt you'll be working with large input files to begin with.
Last edited by Plasmator; September 23rd, 2009 at 11:44 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|