2014-04-18 2 views
-1

아래의 문제에서 약간의 오류가 발생합니다. 다음과 같이 문제 문은 다음과 같습니다두 파일을 함께 병합합니다.

이 라인의 핵심 데이터 필드에 의해 정렬됩니다 두 개의 입력 파일을 읽는 프로그램을 작성합니다. 프로그램은이 두 파일을 병합하여 키 필드에 의해 정렬 된 두 파일의 모든 행을 포함하는 출력 파일 을 작성합니다. 예제로 두 개의 입력 파일에 이름순으로 정렬 된 특정 클래스의 학생 이름과 성적이 포함 된 경우 아래에 표시된 정보를 병합하십시오.

파일 1 :

Adams C 
Jones D 
King B 

파일 2 :

Barnes A 
Johnson C 

출력 파일 :

Adams C 
Barnes A 
Johnson C 
Jones D 
King B 

한 번에 한 줄의 파일을 읽고 다른 파일에서 읽은 행 줄을 출력 파일로 읽어야합니다. 다음은 내가 여기에 잘못된 헤더 파일을 사용하고 내가

C:\Users\Ndekwu\Downloads\merge.cpp||In function 'void merge()':| 
C:\Users\Ndekwu\Downloads\merge.cpp|5|error: 'ifstream' was not declared in this scope| 
C:\Users\Ndekwu\Downloads\merge.cpp|5|note: suggested alternative:| 
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iosfwd|159|note: 'std::ifstream'| 
C:\Users\Ndekwu\Downloads\merge.cpp|5|error: expected ';' before 'ifile1'| 
C:\Users\Ndekwu\Downloads\merge.cpp|6|error: expected ';' before 'ifile2'| 
C:\Users\Ndekwu\Downloads\merge.cpp|7|error: 'ofstream' was not declared in this scope| 
C:\Users\Ndekwu\Downloads\merge.cpp|7|note: suggested alternative:| 
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iosfwd|162|note: 'std::ofstream'| 
C:\Users\Ndekwu\Downloads\merge.cpp|7|error: expected ';' before 'ofile'| 
C:\Users\Ndekwu\Downloads\merge.cpp|11|error: 'ifile1' was not declared in this scope| 
C:\Users\Ndekwu\Downloads\merge.cpp|12|error: 'ifile2' was not declared in this scope| 
C:\Users\Ndekwu\Downloads\merge.cpp|14|error: 'eof' was not declared in this scope| 
C:\Users\Ndekwu\Downloads\merge.cpp|14|error: expected ')' before 'AND'| 

무엇입니까 오류는

Read a line from each data file 
While the end of both files has not been reached 
    If the line from file 1 is smaller than the line from file 2 
      Write the line from file 2 to the output file and read a new line from file 1 
    Else 
      Write the line from file 2 to the output file and read a new line from file 2. 
Write the remaining lines (if any) from file 1 to the output file. 
Write the remaining lines (if any) from file 2 to the output file. 

 

#include <fstream.h> 
#include <iostream.h> 

using namespace std; 

void merge() { 
    ifstream ifile1("input1.txt"); 
    ifstream ifile2("input2.txt"); 
    ofstream ofile("output.txt"); 
    std::string temp1; 
    std::string temp2; 

    ifile1.getline(temp1, 100); 
    ifile2.getline(temp2, 100); 

    while (ifile1 != EOF AND ifile2 != eof) { 
    while (temp1[i++] != "\n") 
     ; 
    while (temp2[j++] != "\n") 
     ; 
    if (i < j) { 
     ofile << temp2; 
     ifile1.getline(temp1, 100); 
    } else { 
     ofile << temp1; 
     ifile2.getline(temp2, 100); 
    } 
    } 
    if (ifile1 != eof) { 
    ifile1.getline(temp1, 100); 
    ofile << temp1; 
    } 
    if (ifile2 != eof) { 
    ifile2.getline(temp1, 100); 
    ofile << temp1; 
    } 
} 

int main() { 
    merge(); 
} 

있습니다 : 일반적인 병합 알고리즘은 다음과 같다?

세미콜론
라인 while 후 :

while(temp1[i++]!="\n"); 
         ^This could be a problem. 

이 기능이 없습니다

+0

무엇을 시도 했습니까? 그 오류는 당신에게 당신에게 제안을 해주고 있습니다, 왜 그것을 수정하지 않습니까? 또한 C++로 프로그래밍하지만 C 스타일 헤더를 사용합니다.또한 "std"를 명시 적으로 사용하거나 사용하지 않고 번갈아 사용하고 있습니다. 일반적으로 허용되는 가장 좋은 방법은 항상 "std ::"를 사용하고 "using namespace std;"를 사용하지 않는 것입니다. –

답변

관련 문제