如何更改文本文件中序列的字母?

How can I change the letters of a sequence in a text file?

我必须改进和扩展这段代码。详细地说,我有一个包含基因型代码的文本文件(即 AGGGGCCCTATTCGCCC.....),我想像这样更改这些代码:

A -> T

G -> C

C -> G

T -> A

我的意思是像上面那样把A改成T。然后我将这个新代码保存在我的文件中。

如果你能指导我解决这个问题,我将不胜感激。

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

int readFile (std::string Genotype, std::vector<std::string>& fileContent)
{

    // Opening the Genotype file
    std::ifstream CGenotype("AT.txt");

    // Checking if object is valid
    if (CGenotype.fail())
    {
        std::cout << "Cannot open the Genotype File : " << Genotype << std::endl;
        return EXIT_FAILURE;
    }

    if (CGenotype.peek() == std::ifstream::traits_type::eof())
    {
        std::cout << "The file is empty: " << Genotype << std::endl;
        return EXIT_FAILURE;
    }
    std::string str;
    // Reading the next line from genotype file untill it reaches the end.
    while (std::getline(CGenotype, str))
    {
        // Line contains string of length > 0 then save it in vector
        if (str.size() > 0)
        {
            fileContent.push_back(str);
        }
    }
    //Closing the genotype file
    CGenotype.close();
    return EXIT_SUCCESS;
}

int writeFile (std::string Genotype, std::vector<std::string>& fileContent)
{
    std::string str;
    while (std::getline(CGenotype, str))
    {
    if (str== 'A';
    cout << 'T';
    else if (str== 'T';
    cout << 'A';
    else if (str== 'C';
    cout << 'G';
    else if (str== 'G';
    cout << 'C';
    }
    CGenotype.close();
} 
int main()
{
    std::vector<std::string> fileContent;

    // Getting the contents of genotype file in a vector
    int fileCheck = readFile("AT.txt", fileContent);

    if (!fileCheck)
    {
        // Printing the vector contents
        for (std::string& line : fileContent)
            std::cout << line << std::endl;
    }
}

这样的事情怎么样?此版本处理每个字符,而不是每一行。为了简短起见,我没有包含任何特定于域的错误处理。

我假设您想处理每个单独的字符...并且每个字符都被内联替换或保持原样。

int main() {
    // ... Open the file (will default to both read and write)
    std::fstream s("AT.txt");
   
    // ... Get initial position (i.e., 0)
    long pos = s.tellp() ;

    // ... Repeat: read a character until you can't  
    while ( s.seekp(pos++) ) {
        // ... Parse the current character
        switch( s.peek() ) {
        case 'A': s.write("T", 1); break ; // ... replace inline
        case 'G': s.write("C", 1); break ; // ... replace inline
        case 'C': s.write("G", 1); break ; // ... replace inline
        case 'T': s.write("A", 1); break ; // ... replace inline
        default:                   break ; // ... nothing to translate
        }
    }
    // .... File will close automagically
    return EXIT_SUCCESS ;
}

我在想这样的事情(在适当的地方嵌入解释):

#include <algorithm>
#include <iterator>
#include <fstream>
#include <filesystem>
int main()
{
    {
        // open input and disposable temporary output file
        std::ifstream in("in.txt");
        std::ofstream out("out.txt");
        
        //read character from input file, write transformed character to output file
        std::transform(std::istream_iterator<char>(in),
                         std::istream_iterator<char>(),
                         std::ostream_iterator<char>(out),
                         [](char val)
                         {
                             switch(val)
                             {
                                 case 'A': return 'T';
                                 case 'G': return 'C';
                                 case 'C': return 'G';
                                 case 'T': return 'A';
                                 default: return val;
                             }
                         });
    } // RAII closes open files here
    
    // replace input file
    std::filesystem::remove("in.txt"); 
    std::filesystem::rename("out.txt", "in.txt");
}

没有像其他答案那样就地转换文件的理由:如果出现任何问题,直到输入文件被输出文件替换,输入文件才不会受到损坏。在失败的情况下,window 像半转换文件这样的损坏是最小的。