Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何编写C++;当行数为>;时清除文本文件的程序;30_C++_File_Text - Fatal编程技术网

C++ 如何编写C++;当行数为>;时清除文本文件的程序;30

C++ 如何编写C++;当行数为>;时清除文本文件的程序;30,c++,file,text,C++,File,Text,这个程序将一些东西打印到文本文件中。我想删除文本文件的内容时,行数超过30,然后再次开始打印内的文本文件 void GPSCallback(const gps_common::GPSFix::ConstPtr& msg) { std::ofstream output; output.open("file1.txt",std::ios_base::app); output<<"\n"<<msg->latitude<<","&

这个程序将一些东西打印到文本文件中。我想删除文本文件的内容时,行数超过30,然后再次开始打印内的文本文件

void GPSCallback(const gps_common::GPSFix::ConstPtr& msg)
{
    std::ofstream output;
    output.open("file1.txt",std::ios_base::app);
    output<<"\n"<<msg->latitude<<","<<msg->longitude;
    output.close();
    usleep(10000000);
}
void GPSCallback(const gps_common::GPSFix::ConstPtr&msg)
{
std::流输出;
output.open(“file1.txt”,std::ios\u base::app);

输出可以在调用
GPSCallback的函数中使用如下计数器

if(++count < 30)
    GPSCallback(msg);
 else{
   count =0;
   std::ofstream output;
   output.open("file1.txt");
   output.close();
   GPSCallback(msg);
 }
if(++计数<30)
GPSCallback(msg);
否则{
计数=0;
std::流输出;
output.open(“file1.txt”);
output.close();
GPSCallback(msg);
}
试试这个

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string name;
    int count=0;
    output.open("file1.txt",std::ios_base::app);
    if (output.is_open()) {
    while (!output.eof()) {

      getline(name); 
      count++;
   }
   }
   if(count>=30)
   {
     // do whatever you want
   }
   output.close();
}
#包括
#包括
使用名称空间std;
int main()
{
字符串名;
整数计数=0;
output.open(“file1.txt”,std::ios\u base::app);
if(output.is_open()){
而(!output.eof()){
getline(名称);
计数++;
}
}
如果(计数>=30)
{
//你想干什么就干什么
}
output.close();
}

一旦达到30行限制,可以使用seekp重置指向文件开头的指针。这不会删除文件的内容,但会被覆盖。

当您写入30行时,只需关闭并重新打开?我希望它自动执行。除此之外,我还使用了append函数,因此如果我按照您的操作,它将无法工作我可以知道文本文件的内容在哪里被删除吗?