繁体   English   中英

c++ 如何从文本文件中的特定行读取和写入?

[英]c++ how to read and write from a specific line in a textfile?

您好我正在尝试从文本文件更新中读取特定行并将其放回同一行而不影响 c++ 中的其他行我正在尝试执行代码并在我重新执行时添加值

#include <iostream>
#include <stream>
#include <stdio.h>
#include <string>
#include <stream>

using namespace std;
void stringGen(char num){
ifstream ifile;
ifile.open("example1.txt");
if(ifile) {

int LINE = 5;
string line;
 ifstream myfile1 ("example1.txt");
   for (int i = 1; i <= LINE; i++)
     getline(myfile1, line);

     cout << line<<endl;

 stringstream geek(line);
 int num=0;
 geek>>num;
 if(num<61004){
    num=num+1;
    ofstream MyFile("example1.txt");//
    
    
    MyFile.close(); 
    }


else{
    num=61001;
    ofstream MyFile("example1.txt");//
    MyFile << num;
    MyFile.close(); 
}
}
else{
    int num=61001;
    cout<<num<<endl;
    ofstream MyFile("example1.txt");//
    MyFile << num+1;
    MyFile.close(); 
}


}
int main (){
char num;
stringGen(num);
return 0;  
}

首先,您需要了解如何存储带有行的文件。 简而言之,它是一个字节序列,一个字节一个接一个。 在这个字节序列中可能有一些特殊字符,人们可以将其解释为行尾,例如'\n'。 但其他字符甚至多个字符也是可能的:

如果你看下面的文字。

Hello1
World1
Hello2
World2

它可能存储在这样的文件中:

Hello1\nWorld1\nHello2\nWorld2\n

仅仅因为我们将 '\n' 解释为行尾,我们就可以“看到”其中的行。

所以,如果你想修改一行,那么你需要在文件中找到我们解释为一行的东西的开始position,然后修改一些字节。

当然,只有在“线”的长度不会改变的情况下才能做到这一点。 然后你可以使用“seek”函数并覆盖所需的字节。

实际上,没有人会这样做。 通常,您会将文件的“行”读入某种 memory 缓冲区,然后在那里进行修改,然后写回所有行。

例如,您将定义一个std::vector然后读取所有行,方法是使用std::getlinepush_back中的行std::vector

修改将在std::vector中完成,所有数据将被写回文件,覆盖所有“旧”数据。

这个问题还有更多的答案。 如果您有更具体的问题,我会再次回答

一些简单的示例代码

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main() {

    // Here we will store all lines of the text file
    std::vector<std::string> lines{};

    // Open the text file for reading and check, if it could be opened
    if (std::ifstream textfileStream{ "test.txt" }; textfileStream) {


        // Read all lines into our vector
        std::string oneLine{};
        while (std::getline(textfileStream, oneLine)) {

            // Add the just read line to our vector
            lines.push_back(oneLine);
        }

        // For test purposes, modify the first line
        if (not lines.empty()) lines[0] = "MODIFIED";
    }
    else std::cerr << "\nError: Could not open input text file\n";


    // Write back data
    // Open the text file for writing and check, if it could be opened
    if (std::ofstream textfileStream{ "r:\\test.txt" }; textfileStream) {

        // Iterate over all lines and wriite to file
        for (const std::string& oneLine : lines)
            textfileStream << oneLine << '\n';
    }
    else std::cerr << "\nError: Could not open output text file\n";

    return 0;
}
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;
void stringGen(char num){
int count=0;
int a;
string line,check,linex;
string msg="Message_Handler:";
fstream ifile;
ifile.open("sample.txt",ios::in|ios::out);
if(ifile){
while(getline (ifile,line)) {
    if (line.find("Message_Handler:") == 0){
        check=line.substr(16,5);
        count++;
        a=ifile.tellp();
    }
    }
 ifile.close();
 if(count==0){
    int num=61001;
    cout<<num<<endl;
    num=num+1;
    ofstream examplefile ("sample.txt",ios::app);
    examplefile<<"Message_Handler:"<<num;
    examplefile.close();
 }
  if(count==1){
    cout<<check<<endl;  
    stringstream geek(check);
    int num=0;
    geek>>num;
    if(num<61004){
        num=num+1;
        stringstream ss;
        ss << num;
        string nums = ss.str();
        fstream MyFile("sample.txt",ios::in|ios::out);
        MyFile.seekp(a-5);
        MyFile<<nums;
  }
   else{
    int num=61001;
    stringstream ss;
        ss << num;
        string nums = ss.str();
        fstream MyFile("sample.txt",ios::in|ios::out);
        MyFile.seekp(a-5);
        MyFile<<nums;

   }
   }
  }
else{
int num=61001;
cout<<num<<endl;
ofstream MyFile("sample.txt",ios::app);
MyFile <<"Message_Handler:"<< num+1;
MyFile.close(); 
}
}
int main (){
char num;
stringGen(num);
return 0;  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM