The traditional problem with reading & writing files with iostream...
I try to read "slav.txt" file, then paste this in "slav2.txt".

The text in "slav.txt" is "Hello world", at console i receive "Helloworldrl" (with nospace and "rl" at end), and in "slav2.txt" i receive "drl"

This is my code:

#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;

class File
{
      ifstream f1;
      ofstream f2;
      char s[200];
      
      public:
             void ShowFile(char *nume);
             void ReadInFile(char *nume);
};

void File::ReadInFile(char *nume)
{    
  
     f2.open(nume);  
     for(int i=0; i<strlen(s); i++)
     {  
     f2<<s[i];
     }
     
      f2.close();
}

void File::ShowFile(char *nume)
{
     f1.open(nume);
     if(f1.fail())
     {
                  cout<<"Error opening file!";
                  getch();
                  exit(1);
     }
     while(!f1.eof())
     {
                     
                     for(int i=0; i<strlen(s); i++)
                     {
                             f1>>s[i];
                             cout<<s[i];
                     }
     }
     f1.close();
}
             
int main()
{
    File F;
    F.ShowFile("slav.txt");
    F.ReadInFile("slav2.txt");
    
     getch();
     return 0;
}

Recommended Answers

All 6 Replies

hmm... your code has me thoroughly confused, i wont lie... why not try something like including putting
#include<io.h> at the top...
then when you get to copying the things,
then throw in CopyFile("slav1.txt","slav2.txt",TRUE)... this should work, although I heard it wont work if you use linux... hope I helped somewhat :P

ShowFile() is coded incorrectly. This is how to read the file one character at a time. This will only read the first sizeof(s) characters, so if the file consists of more lines it may not read the whole file.

memset(s, 0, sizeof(s)); // clear the array of all junk data
     i = 0;
     while(f1 >> s[i] && i < (sizeof(s)-1))
     {
           cout << s[i];
           i++;
      }

But a much easier way to code it and it will read the whole file regardless of how may line the file contains.:

while( f1.getline(s, sizeof(s)) )
  {
      cout << s << "\n";
  }

Thank you!
But, tell me please, in the first method why the blank character (the space) is omited by default?
Is there one error, or such work the method?

The most intriguing question on this forum: how pupils did their homeworks in pre-Internet era?
In the "1st method" you had incorrect, erratical behaviour: you get strlen(s) where s is not initialized (then overwritten with your program char by char input). Better forget this nightmare and follow Ancient Dragon's advice(s)...

Thank you!
But, tell me please, in the first method why the blank character (the space) is omited by default?
Is there one error, or such work the method?

The extraction operator >> skips over whitespace. To read a file character by character including whitespace, use the get() method. Though I would just listen to Ancient Dragon.

To read data from file was just one step of problem. The second step should be how to skip whitespace! However i get all 2 in 1 and solved the problem one time, i just wanted tu be sure if here is correct!
And "The extraction operator >> skips over whitespace" it's enough to understand!
Thank's to all!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.