print first 50 spaces of each line of the file

Hello,
I'm trying to complete a problem that asks to print out the first 50 spaces of each line. I can print out the whole file, but I don't know how to print the first 50 spaces. Any help will be greatly appreciated.

Correction: I need to print the words in the first 50 spaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
using namespace std;

int main () 
{   
      int length;
      char *buffer;
      string inputBuffer;
    
      ifstream fin;
      fin.open ("trekkie", ios::binary);
      
      fin.seekg (0, ios::end);
      length = fin.tellg();
      fin.seekg (0, ios::beg);
    
      buffer = new char [length];
    
      fin.read (buffer,length);
      
      do {
         getline(fin, inputBuffer);
         cout.write (buffer,length);
      } while (!fin.eof());
    
      fin.close();
    
      delete[] buffer;
      system("PAUSE");
      return 0;

}
Last edited on
First, you need to learn how to use the string class and getline function.
http://cplusplus.com/reference/string/getline/
http://cplusplus.com/reference/string/string/

Instead of reading the file in binary mode, read it in text mode so that you can read 1 line into a string. Then loop until the file is processed. For each line that is read into the string, print the lesser of the number of characters in the line or the first 50 characters. From what you are describing it seems that you are working with a text file, not a binary file.
i understand how to use string and getline function, but I don't know how to manipulate it to print the first 5o characters. How do I read a line into a string?
Is text mode just removing the ios::binary from the code?

can you provide a sample code?

Doing what you said, i have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
using namespace std;

int main () 
{   
      int length;
      char *buffer;
      string inputBuffer;
    
      ifstream fin;
      fin.open ("trekkie");
      
      fin.seekg (0, ios::end);
      length = fin.tellg();
      fin.seekg (0, ios::beg);
    
      buffer = new char [length];
    
      fin.read (buffer,length);
      
      do {
         getline(fin, inputBuffer);
         cout.write (buffer,length);
      } while (!fin.eof());
    
      fin.close();
    
      delete[] buffer;
      system("PAUSE");
      return 0;

}
First of all, get rid of these lines
1
2
3
4
5
6
7
      fin.seekg (0, ios::end);
      length = fin.tellg();
      fin.seekg (0, ios::beg);
    
      buffer = new char [length];
    
      fin.read (buffer,length);


Your while loop is on the write track. After reading each line, you have to determine the string size. The string::size function tells you that. Or you could simply do another loop until 50 characters are printed or the end of the string is reached. Put this loop inside your do..while and get rid of your cout.write statement.
1
2
3
4
5
while(current < 50 && current < inputBuffer.size())
{
    cout << inputBuffer[current];
}
cout << endl;


Last edited on
Thanks alot for taking the time to help me.
I now have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main () 
{
      int current;
      string inputBuffer;
    
      ifstream fin;
      fin.open ("trekkie.txt");
            
      do {
          getline(fin, inputBuffer);          
          while(current < 50 && current < inputBuffer.size())
          {
                       cout << inputBuffer[current];
          }
          cout << endl;
      } while (!fin.eof());
    
      fin.close();
    
      system("PAUSE");
      return 0;

}


But I'm getting only blank spaces. None of the information on the text file was printed. What should I do next?

Thanks alot
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main () 
{
    string inputBuffer;

    ifstream fin;
    fin.open ("trekkie.txt");

    do {
	int current(0);
	getline(fin, inputBuffer);          
	while(current < 50 && current < inputBuffer.size())
	{
	    cout << inputBuffer[current++];
	}
	cout << endl;
    } while (!fin.eof());

    fin.close();
    return 0;
}


What I had posted before was just the general idea. You actually have to initialize the current variable prior to using it and increment it within the loop. Beginners need to realize that many times we are just posting hints to get you going but you do have to use a little common sense when incorporating those ideas into your program.

By the way, I tried this with a two line text file containing:
This line is less than 50 characters.
This line is definitely going to contain a lot more than 50 characters.
Last edited on
Topic archived. No new replies allowed.