| |

VerySource

 Forgot password?
 Register
Search
Author: fcuptfwfn

File input and output problems

[Copy link]

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-12-19 22:15:02
| Show all posts
The above examples are selected from Chapter 13 Section 10 of the C++ Standard Library. The main floor has a comprehensive understanding of the relationship between stream and streambuf. You can read this book
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-12-20 09:15:01
| Show all posts
But some books say that fstream fio; is to create an input and output file stream object, that is, you can use this object for input and output operations~~~~? Don't understand—_—!
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-20 10:45:01
| Show all posts
Use fstream like this

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    fstream myfile;
    myfile.open("a.txt",ios::out|ios::app);
    if(!myfile)
    {
        cout<<"File error!"<<endl;
        system("pause");
        exit(1);
    }
    myfile<<"new add "<<endl<<"URL;
    myfile.close(); //Pay attention to close
  
    myfile.open("a.txt",ios::in);
    if(!myfile)
    {
        cout<<"File reading error!"<<endl;
        system("pause");
        exit(1);
    }
    char ch;
    while(myfile.get(ch))
    {
        cout.put(ch);
    }
    myfile.close();
    system("pause");
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-20 11:00:02
| Show all posts
Your question is the same as the example I gave

Remember to close the pointer after adding
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-20 11:15:01
| Show all posts
Is the file pointer operation problem:
#include<iostream>
//#include<conio.h>
#include<fstream>
using namespace std;

int main()
{
        fstream file("a.txt",ios::in|ios::out);
        string s1,s2;

        s1="abcd 1234\n";
        file<<s1; //Write characters into the file, and the file pointer also points to the end of the file
        file.seekg(ios_base::beg);//Move the file pointer back to the head of the file
        file>>s2;
        cout<<"s2="<<s2<<endl;//s2 is empty

        file.close();
        //getch();
        return 0;
}

The output is:
s2=abcd //Because the read is not read by line, it is distinguished by spaces
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-12-21 02:00:01
| Show all posts
Thanks:fangjiehong,firegun,nimasile1
I understand all the programs you wrote, but the program I wrote (the one that only defines the fstream stream) compiles and runs without error, but it just can’t output s2 (a.txt was not created at all), so write it with you I have done a comparison of the programs or I don’t know why the a.txt text is not created? Can you analyze it for me? Thank you~~~~~

The program ofnimasile1uses an open() when writing text, and close() after writing. When reading variables, another open and then close(). Although the purpose is achieved, if you want to read and write repeatedly in a loop , I don’t feel very good.

The program offiregunis very good, but he didn't use ifstream, it used filebuf, ostream and istream.

The program ofliebersieis almost the same as mine, but after I run it, I still don’t get the s2 result (the output is empty and the text is not created). It should not be related to the compiler, right?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-21 08:45:01
| Show all posts
Is it exactly the same? Although only one line is missing, the effect is different.
I compile with g++ under Linux and compile with VS under Windows.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list