2017-06-22 6 views
0

Ich möchte das erste Zeichen in der Eingabedatei ausdrucken ist das letzte Zeichen in der Ausgabedatei und umgekehrt. Aber ich blieb stecken, um die Ausgabe auszudrucken.Das erste Zeichen in der Eingabedatei ist das letzte Zeichen in der Ausgabedatei und umgekehrt

Ich brauche Arrays zu verwenden. Ich werde aus der Eingabedatei in ein Zeichenfeld lesen und das Schreiben vom Array in die Ausgabedatei.

Beispiel:

Eingabe.txt: A B C D E H

output.txt: H B C D E A

This is my code 

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

    using namespace std; 

    int main() 
    { 
     string FileName, FileName2; 
     string s, temp, FirstChar, LastChar;; 
     char again = 'Y'; 
     bool close = false; 
     char MAXSIZE[1024]; 
     while (close == false) 
     { 
      cout << "Open the file: "; 
      cin >> FileName; 
      ifstream ReadFromFile(FileName); 
      if (ReadFromFile.is_open()) 
      { 
       cout << "Succeed to open the file!\n"; 

       // Read character from the input to array 
       while (!ReadFromFile.eof()) 
       { 
        ReadFromFile >> MAXSIZE; 
        cout << MAXSIZE << " "; 
       } 
       cout << endl; 

       cout << "Enter the first character: "; 
       cin >> FirstChar; 
       cout << "Enter the last character: "; 
       cin >> LastChar; 
       swap(FirstChar, LastChar); 

    // I stuck at here 

       ifstream in(FileName); 
       cout << "Enter a name for a copy file: "; 
       cin >> FileName2; 
       ofstream out(FileName2); 
       while (getline(in, s)) 
         out << s << "\n"; 


       cout << "Close the program and then open your copy file."; 
       cout << endl << endl; 
       close = true; 
      } 
      else{ 
       cout << "Failed to open the file!\n"; 
       do { 

        cout << "Do you want to do it again(Y) or Close (N)? "; 
        cin >> again; 
       } while (again != 'y' && again != 'Y' && again != 'n' && again != 'N'); 

       if (again == 'y' || again == 'Y') 
        close = false; 
       else 
        close = true; 

       cout << endl; 
      } 
     } 

     system("pause"); 
     return 0; 
    } 
+0

, wenn die Datei nicht groß, wirklich alle, rückwärts, und Schreib zurück. –

Antwort

0

Ihre Aufgabe (nach Ihrer Erklärung) erfordert: das erste und das letzte Zeichen

1) das Lesen von Eingabedatei

2) zum Ändern Array

3) Speichern von Arrays Ausgabedatei

012.

Also, die ersten und letzten Zeichen sollten nicht von der Standardeingabe (Tastatur) gefragt werden.

#include <iostream> 
#include <fstream> 

using namespace std; 

// filenames can be given as command line arguments 
// change the code if you want to read them from standard input 
int main(int argc, char* argv[]) 
{ 
    ifstream inf; 
    ofstream outf; 
    size_t counter = 0; 
    const size_t MAXSIZE = 1024; // MAXSIZE - name of constant 
    char buffer[MAXSIZE];  // buffer - name of array 
    // check the command line arguments 
    // Alternatively you can define: string InpFileName, OutFileName; 
    // as it is in your code and enter values (cin >>) instead using argv 
    // if so, you should change inf.open(argv[1]); to inf.open(InpFileName); 
    // and outf.open(argv[2]); to outf.open(OutFileName); 
    if (argc != 3) 
    { 
     cerr << "Two arguments are required:" << endl 
      << " 1) name of existing file (to read)" << endl 
      << " 2) name of new file (to create)" << endl; 
     return 1; 
    } 
    // open files 
    inf.open(argv[1]); 
    outf.open(argv[2]); 
    // check files 
    if (!inf.is_open() || !outf.is_open()) 
    { 
     cout << "ERROR: some trouble with files." << endl; 
     return 2; // stop the program 
    } 
    // process 
    // 1) reading 
    while (counter < MAXSIZE){ 
     buffer[counter] = inf.get(); 
     if (buffer[counter] != EOF) 
     { 
      counter++; 
     } 
     else 
     { 
      counter--; // the last character is not valid 
      break; // end of file 
     } 
    } 
    // 2) changing 
    char b = buffer[counter]; 
    buffer[counter] = buffer[0]; 
    buffer[0] = b; 
    // 3) output 
    for (int i = 0; i <= counter; i++) 
    { 
     outf.put(buffer[i]); 
    } 
    // close files 
    inf.close(); 
    outf.close(); 
    return 0; 
} 

UPDATE:

die Aufgabe, für die Fälle, Clarify, wenn einige nicht druckbare Zeichen (wie Leerzeichen) ist die erste oder die letzte

+0

Ich steckte im 3. Schritt –

+0

Können Sie es einfacher machen, weil ich nur ein Anfänger der Programmierung –

+0

Ich habe einige Kommentare hinzugefügt und Code geändert, um es einfacher zu machen – VolAnd

-1

Sieht wie ein Hausaufgaben Problem für eine C++ Klasse. Ein Hinweis, die dazu beitragen können, ist zu teilt die Datei in Blöcke von X Bytes und lesen Sie die Blöcke in umgekehrter Reihenfolge ....

std :: istream :: seekg

Verwandte Themen