我正在尝试将从一个程序读取的字符串内容用于另一个程序。我已经放了一些示例代码来重现这个例子。我有一个main
函数
//main.cpp
#include "read.h"
#include "read_fasta.h"
#include <iostream>
#include <string>
int main(int argc, char **argv)
{
index();
read_fasta_file(argc,argv);
std::cout << " : " << content << std::endl;
std::cout << "Length of fasta file: " << content.length() << std::endl;
return 0;
}
我看了fasta
档
//read_fasta.cpp
#include <iostream>
#include <fstream>
#include "read_fasta.h"
std::string content;
int read_fasta_file(int argc, char **argv)
{
std::ifstream input(argv[1]);
std::string line, name;
while( std::getline( input, line ).good() ){
if( line.empty() || line[0] == '>' ){ // Identifier marker
if( !name.empty() ){ // Print out what we read from the last entry
std::cout << name << " : " << content << std::endl;
name.clear();
}
if( !line.empty() ){
name = line.substr(1);
}
content.clear();
} else if( !name.empty() ){
if( line.find(' ') != std::string::npos ){ // Invalid sequence--no spaces allowed
name.clear();
content.clear();
} else {
content += line;
}
}
}
if( !name.empty() ){ // Print out what we read from the last entry
std::cout << name << " : " << content << std::endl;
std::cout << "Length of fasta file: " << content.length() << std::endl;
}
return 0;
}
read_fasta.cpp
//read_fasta.h
#ifndef read_fasta_h
#define read_fasta_h
#include <iostream>
#include <string>
#include <fstream>
int read_fasta_file(int argc, char **argv);
extern std::string content;
#endif
直到现在每件事都运转良好。现在,我尝试在另一个std::string content
程序中使用cpp
。
//read.cpp
#include <iostream>
#include <string>
#include "read.h"
void index()
{
std::cout<<"Do U see any printing :"<<content<<std::endl;
}
read.cpp
//read.h
#ifndef read_h
#define read_h
#include <iostream>
#include <string>
#include <vector>
void index();
extern std::string content;
#endif
但std::string content
计划中的read.cpp
似乎没有任何内容。我错过了什么。