Thread: string from file

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    string from file

    i'm supposed to read strings from a data file that contain words seperated by numbers, i'm supposed to convert the numbers to spaces and reverse the list of stings when i print them to the screen, so they make a paragraph
    (meaning the bottom string in the data file should be printed as the first, and so on)

    here is the information in the data file:

    the1text,2you3should4look5at6that7as8well.
    Likewise,9if8there7are6numerals5mixed4throughout
    321
    order,1then2you3still4have5some6work7to8do.
    of9pieces8of7sentences6with5the4parts4out3of
    If,2on1the1other2hand,3this4seems5to6be7a8jumble
    0
    other.494
    sentences1which2flow3logically4from5one6to7the
    that8it9makes8sense7I6mean5that4it3contains
    then2your1program1most2likely3works.44When5I6say
    If,7when8you9read8this,7it6seems5to4make3sense
    i've gotten almost nowhere with my code, here's all i got

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main ()
    {
      
      char buffer[256];
      ifstream input ("A:\input4.dat");
    
      while (! input.eof() )
      {
        input.getline (buffer,100);
        char getchar;
        cin>>getchar;
        if(isdigit(getchar))
        cout<<" ";
        cout << buffer << endl;
    
      }
      
     return 0;
    }
    looking for urgent help as assignment is due very soon
    thanx,

    Isaiah

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    welcome to the boards!
    Well, I'd have to say first off that you'll not want to output everything as you're going along, because you need to flip the sentences around in reverse. What I would do is read the whole file in to a buffer and basically parse it for the numbers and chop 'em out, slap in some spaces, then reverse the sentences and you should be good.

    I don't believe in doing homework for other people, so hopefully that helps you a little bit

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    Quote Originally Posted by jverkoey
    welcome to the boards!
    Well, I'd have to say first off that you'll not want to output everything as you're going along, because you need to flip the sentences around in reverse. What I would do is read the whole file in to a buffer and basically parse it for the numbers and chop 'em out, slap in some spaces, then reverse the sentences and you should be good.

    I don't believe in doing homework for other people, so hopefully that helps you a little bit
    is there a "replace" function for strings where it can find an integer 0-9 and just replace it with a space?

    thanx

    Isaiah

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    there might be one, but i don't know of it, it would probably be better to just write your own "replace" function that you could just pass a char* and it would remove all of the numbers

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <stdlib.h>
    using namespace std;
    
    string space(string data);
    string openfile();
    string reverse(string data);
    
    int main()
    {
    	string spaces;
    	string infile;
    	string reversed;
    	infile = openfile();
    	spaces = space(infile);
    	reversed = reverse(spaces);
    	cout << "original data: " << infile << "\n\n";
    	cout << "without numbers: " << spaces << "\n\n";
    	cout << "reversed: " << reversed << "\n\n";
    	return 0;
    }
    
    string space( string buffer )
    {
    	long x = 0;
    	int number = 48;
    	while(x < buffer.length())
    	{
    		number = 48;
    		while(number != 58)
    		{
    			if(buffer[x] == (char)number){
    				buffer[x] = ' ';
    			}
    			number++;
    		}
    		x++;
    	}
    	
    	return buffer;
    }
    
    
    string openfile()
    {
        string buffer;
    	ifstream file("input4.dat");
    	
    	if (! file.good() ) {
    		buffer = "error opening file!\n";
    	}
    	else {
    		file >> buffer;
    		file.close();
    	}
    	return buffer;
    }
    
    string reverse(string buffer)
    {
        for ( int x = 0, y = buffer.length() -1; x < y; x++, y-- ) 
    	{ 
            int temp =  buffer[x]; 
            buffer[x] = buffer[y]; 
            buffer[y] = temp; 
        } 
    	return buffer;
    }
    this what you want...?

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Don't do peoples' homework for them.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Don't do peoples' homework for them.
    And if you're going to then at least do a good job of it.

    >i'm supposed to convert the numbers to spaces and reverse the list of stings
    You want to read a line, convert the delimiters to spaces, then save the line to an array. Continue to do this until you run out of lines and then print the array in reverse. Here is an algorithm for part 1 (in pieces):
    Code:
    char buffer[256];
    char lines[10][256];
    int i = 0;
    
    while ( cin.getline ( buffer, sizeof buffer ) && i < 10 ) {
      strcpy ( lines[i++], convert ( buffer ) );
    }
    
    char *convert ( char buffer[] )
    {
      for ( int i = 0; buffer[i] != '\0'; i++ ) {
        if ( isdigit ( buffer[i] ) ) {
            buffer[i] = ' ';
        }
      }
    
      return buffer;
    }
    And an algorithm for part 2:
    Code:
    while ( i >= 0 ) {
      cout<< lines[i--] <<endl;
    }
    >is there a "replace" function for strings where it can find an integer 0-9 and just replace it with a space?
    If you have the new headers you can use replace_if in the <algorithm> header. Otherwise you're pretty much on your own.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM