Home > Back-end >  Read a file, gibberish, for help
Read a file, gibberish, for help

Time:09-29

#include
#include
#include
#include
#include
using namespace std;
Bool writeandread (string strDir)
{
//ifstream in (strDir);//the structure of the parameters, can save the open function
String path=strDir + "\ \ iotest TXT";
Ofstream file1.//create a file stream object;
File1. Open (path, ofstream: : out);//open the file;
File1 & lt; & lt;" The HelloWorld "& lt; File1 & lt; & lt;" Let 's start coding the world & lt; "" File1. Close ();//close the file flow;

Ifstream in;
In the open (path, ifstream: : in);
String s;
In getline (const_cast & lt; Char * & gt; (s.c _str ()), 20, '/n');
CoutIn the close ();

return true;
}
Void main ()
{
String Path="F: \ \ tongyuan";
/* bool iFlag=createDir (Path);
If (iFlag==false)
return; */
//write_text (Path);
Writeandread (Path);
return;
}
I want to read the document in turn two lines, how to do?
This section of the program, a DOS box there is a lot of garbage characters, why? (about 20, explain)

CodePudding user response:

No one, a great god look at the source program

CodePudding user response:


String s;
S.r esize (21, 0);//to allocate a memory space for the string and fill 0. More attention to the end of a string of 0
In getline (const_cast & lt; Char * & gt; (s.c _str ()), 20, '/n');//if not allocate space, may be garbled,
Cout
Immediately after you write is to read, plus it will be more reliable,
File1 & lt; File1. Close ();//close the file flow;

CodePudding user response:

Thank you for the great god, according to your method changed, no noise, but will not be able to output the complete two lines, only:
The HelloWorld
Let 's start codi
Not behind

CodePudding user response:

refer to the second floor PPower response:
string s.
S.r esize (21, 0);//to allocate a memory space for the string and fill 0. More attention to the end of a string of 0
In getline (const_cast & lt; Char * & gt; (s.c _str ()), 20, '/n');//if not allocate space, may be garbled,
Cout
Immediately after you write is to read, plus it will be more reliable,
File1 & lt; File1. Close ();//close the file flow;

Thank you for the great god, according to your method changed, no noise, but will not be able to output the complete two lines, only:
The HelloWorld
Let 's start codi
Not behind

CodePudding user response:

Read you line up to 20 characters, more than 20 characters, will read more and buffer will allocate more space,

CodePudding user response:

reference 5 floor PPower reply:
you line up to read 20 characters, more than 20 characters, to read more, buffer will allocate more space,

I changed to 100100 0, tried it on, are all the same incomplete string

CodePudding user response:

Open your file, see the write full yet?

CodePudding user response:

Writing, I saw it all, but read it out