簡體   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