Reading contents of text file

Need help reading a txt file into an array to be used in a program

Page 1 of 1

14 Replies - 1670 Views - Last Post: 24 September 2009 - 01:46 PM Rate Topic: -----

#1 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Reading contents of text file

Posted 23 September 2009 - 08:26 AM

// inFix to post fix converter

//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"; 
	

 cout << dirt;
//Gets the inFIx typed but this is acutally getting written to the textfile not what you wrote.

	//gets(infix);
	
   
	char infix[size] = "X = A + B"; // this is where the contents of the text file needs to go.
   
	 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;
		 }
}		 
								


This post has been edited by mister1337: 23 September 2009 - 08:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading contents of text file

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Reading contents of text file

Posted 23 September 2009 - 08:41 AM

Quote

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<vector>
#include<string>
#include <stdlib.h>
#include <stdio.h>
#include<stdlib.h>
#include<fstream>

LOL -- covering all your bases eh?

What is the question, what are you having problems with?

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Reading contents of text file

Posted 23 September 2009 - 09:06 AM

Read from file using a loop


  string line;
 ifstream myfile ("C:\\example.txt");
 while (! myfile.eof() )
	{
	  getline (myfile,line); 
// copy the contents of line to a array 

}



Was This Post Helpful? 0
  • +
  • -

#4 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Re: Reading contents of text file

Posted 23 September 2009 - 09:13 AM

But, the contents are not accessable outside of the loop.
Was This Post Helpful? 0
  • +
  • -

#5 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Reading contents of text file

Posted 23 September 2009 - 09:16 AM

yeah so that's why you need to copy the contents of the line string to a array within the loop
Was This Post Helpful? 0
  • +
  • -

#6 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Re: Reading contents of text file

Posted 23 September 2009 - 09:23 AM

Almost got it, still an error
	string line;
	char arrays[size]; //here is the array
ifstream myfile ("C:\\InFix.txt");
while (! myfile.eof() )
	{
	  getline (myfile,line); 
// copy the contents of line to a array 

   strncpy(arrays, line, sizeof(arrays)-1);  //not sure of the error


}


Was This Post Helpful? 0
  • +
  • -

#7 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Reading contents of text file

Posted 23 September 2009 - 09:30 AM

View Postmister1337, on 23 Sep, 2009 - 08:23 AM, said:

   strncpy(arrays, line, sizeof(arrays)-1);  //not sure of the error



strcpy( arrays , line, a) ;

where a == Maximum number of characters to be copied from source.
Was This Post Helpful? 0
  • +
  • -

#8 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Re: Reading contents of text file

Posted 23 September 2009 - 09:39 AM

Still getting a problem.
  string line;
	int a = 50;// like this?
	char arrays[size]; //here is the array
ifstream myfile ("C:\\InFix.txt");
while (! myfile.eof() )
	{
	  getline (myfile,line); 
// copy the contents of line to a array 

   strcpy(arrays, line, a);  //Hmmmm?


}


Was This Post Helpful? 0
  • +
  • -

#9 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Reading contents of text file

Posted 23 September 2009 - 09:53 AM

the strcpy definition

char * strncpy ( char * destination, const char * source, size_t num );




both source and destination should be char arrays

you can do this to manually copy the characters to a array

   int pos=0;
   string line;
  char c1[1000];

	while (! myfile.eof() )
	{
	  getline (myfile,line);   
int i =0;
char c=' ';

for(; c!='\0'; i++){
	 c=line[i];
	
   if( c== '\0' )   c1[pos] =  ' ';
   else c1[pos] = c;
	 printf("%c" , c1[pos]);
	  pos++;	  
	  }   
	  }




this will solve your problem
Was This Post Helpful? 0
  • +
  • -

#10 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Re: Reading contents of text file

Posted 23 September 2009 - 09:54 AM

Thanks Snowflakes, that is helping
 string line;
	int a = 50;// like this?
	char arrays[size]; //here is the array
ifstream myfile ("C:\\InFix.txt");
while (! myfile.eof() )
	{
	  getline (myfile,line); 
// copy the contents of line to a array 


   char * strncpy ( char * arrays, const char * line, size_t a );// this works!


}

cout << arrays[size]; // THis comes out as Jibberish


Was This Post Helpful? 0
  • +
  • -

#11 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

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

Re: Reading contents of text file

Posted 23 September 2009 - 11:52 AM

YOu need a for to display an array
for(int i=0; i<size;i++){
cout<<arrays[size];
}


Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#12 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Re: Reading contents of text file

Posted 23 September 2009 - 06:03 PM

All I'm getting is more jibberish even with the count.
The really has to be a way that I can import this
  string line;
	int a = 50;// like this?
	char arrays[size]; //here is the array
ifstream myfile ("C:\\InFix.txt");
while (! myfile.eof() )
	{
	  getline (myfile,line); 
// copy the contents of line to a array 


   char * strncpy ( char * arrays, const char * line, size_t a );// this works!


}


import it then use it
  char infix[size] = "X = A + B"; // I would use arrays inplace of infix
   
	 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


here.
Was This Post Helpful? 0
  • +
  • -

#13 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

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

Re: Reading contents of text file

Posted 23 September 2009 - 11:13 PM

Can you post the txt your trying to read?
Was This Post Helpful? 0
  • +
  • -

#14 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Re: Reading contents of text file

Posted 24 September 2009 - 06:41 AM

Here it is

Attached File(s)

  • Attached File  InFix.txt (9bytes)
    Number of downloads: 79

Was This Post Helpful? 0
  • +
  • -

#15 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

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

Re: Reading contents of text file

Posted 24 September 2009 - 01:46 PM

Well this is working i copied your text and it worked gets the X = A + B into the array op ;)
Try moving your text into the folder where your .cpp is.
/* This program will read one line from a text file and save it into a string*/

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

using namespace std;

int main(){
	char op[20];
	ifstream data ("InFix.txt");
	data.getline(op,sizeof(data));
		cout<<op;
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1