Thread: some help

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    57

    some help

    this code compiles alright but it doesnt run right......i simply want to get characters from a file and then output them. anyone see why this doesnt work? i believe it has something to do with the eof marker but i could be wrong. anyways, heres the code.....
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    struct list
    {
    	char data;
    	list *link;
        list *beginning;
        list *current;
    	list *pend;
    };
    
    typedef list* listptr;
    
    void main()
    {
    	string temp;
    	char next;
    	ifstream in_stream;
    	cout << "Enter filename";
    	cin >> temp;
    	in_stream.open(temp.c_str());
    	in_stream.get(next);
    	listptr top;
    	top = new list;
    	top->data = next;
    	top->link = NULL;
    	in_stream.get(next);
    	while(! in_stream.eof())
    	{
    		listptr temp;
    		temp = new list;
    		temp->data = next;
    		temp->link = top;
    		top = temp;
    	}
    	while(top != NULL)
    	{
    		char result;
    	    result = top->data;
    		listptr temp;
    		temp = top;
    		top = top->link;
    		delete temp;
    		cout << result << endl;
    	}
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You are not reading any characters in your loop, you are just adding the second character in the file to your list, forever. You should also check if open succeded. Finally this program, when fixed, will print all characters from a file in reverse order one per line, is this really what you wanted to do?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    oh yea......good call, i must have forgot to add a getnext. actually i'll want to use the characters in the right order but somebody tld me to just use two stacks to accomplish this. is there a better way to do it?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    #include<string>
    #include<fstream>
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    int main() {
        std::string filename;
        cout << "Filename :";
        cin >> filename;
        ifstream in(filename.c_str());
        if(in) {
            std::string all;
            getline(in,all,EOF);
    // string all now contains the entire file.
    // do whatever you want with it
        } else {
            std::cerr << "Error opening file " << filename << endl;
        }
        return 0;
    }
    Last edited by grib; 04-09-2003 at 10:35 AM.

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I believe you could do this in a much simpler manner...if all you need to do is display the characters in an input file I would do something like this:
    Code:
    int main()
    {
         ifstream inStream;
         string fileName;
         char charArray[MAX_SIZE];   //whatever the maxsize might be
         char c;
         int i;
    
         cout << "enter file name to analyze: ";
         cin >> fileName;
    
         inStream.open( fileName.c_str() );  
            //check if file opened OK here, then
    
         while( inStream >> c ){
                  charArray[c]++;  //counts how many times each character appers in file
          }
    
          for( i=33; i<=126; i++ ){ //to get all printable chars
                   if ( charArray[i] > 0 ){ //check if character appears in file    
                   cout << (char)i << ":" << setw(4) << setfill(' ') << charArray[i] << "; " ;
                    }
          }
    }
    hope this helps

    axon
    Last edited by axon; 04-10-2003 at 12:08 AM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed