2016-03-20 19 views
-1

Yazılıma bir metin dosyasına yazma konusunda yardıma ihtiyacım var. Dosyayı başarılı bir şekilde yazmayı başarabildim, ancak program sadece beyaz boşlukları olmayan bir dizeyi saklamama izin veriyor. Yani yardıma ihtiyacım olan şey bir değişken kullanarak bir dosyaya tam bir cümle yazmaktır. 'Tam bir cümle bir .txt dosyasına nasıl girilir C++

İşte programın kodu, temel bir gündem/planlayıcı olması gerekiyor.

~ edit ~ Verileri etkilemeye çalıştığımı söylemeyi unutmuşum. ex: okul ödevinizin ne olduğunu sorar ve "Ödev yapın" yazıp enter tuşuna basın.

-shortened kod

yan Ubuntu terminalde -compiled notları
#include <iostream> 
    #include <fstream> 
    #include <stdlib.h> 
    #include "agenda.h" 

    using namespace std; 

    int main(){ 

    string write; 
    string title; 
    string answer; 
    string school = "sch"; 
    string birth = "bir"; 
    string quit = "quit"; 
    string view = "v"; 
    string append = "a"; 
    string clear = "c"; 
    string back = "b"; 
    string yes = y; 

    cout << "------Welcome User!------\n"; 



       cout << "\nSCHOOL PLANNER\n\n"; 

       cout << "Pick your option: \n\n" << 
         "view(v)\n" << 
         "append(a)\n" << 
         "clear(c)\n" << 
         "back(b)\n"; 
       cin >> answer; 

       if(answer == view){ 

        system("cat ~/Desktop/agenda2/school.txt"); 

       } 
       else if(answer == append){ 

        title = "Date: "; 
        cout << "Date: "; 
        cin >> write; 
        Write_School(title, write); 

        title = "Class: "; 
        cout << "Class: "; 
        cin >> write; 
        Write_School(title, write); 


//where The problem is 
        title = "Assignment: "; 
        cout << "Assignment: "; 
        cin >> write; 
        Write_School(title, write); 

        cout << "[DATA SAVED]"; 
       } 
       else if(answer == clear){ 
        cout << "Are you sure you want to delete the files forever?(y/n)"; 
        cin >> answer; 
        if(answer == yes){ 

         Clear_Text(); 
         system("clear"); 
         cout << "Text file emptied.\n\n"; 
        } 
        else{ 
         cout << "All data saved.\n\n"; 
        } 
       } 
       else{ 
        cout << "[ERROR INVALID ENTRY]\n\n\n"; 
       }  
return 0; 


      } 

//here is the header file 

#ifndef agenda 
#define agenda 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <stdlib.h> 
#endif 
using namespace std; 

int Write_School(string title, string write){ 
    ofstream myfile; 

    myfile.open("school.txt", ios::app); 
    myfile << title; 
    myfile << write; 
    myfile << "\n"; 
    myfile.close(); 
} 

int Clear_Text(){ 
    system("clear"); 

    ofstream myfile; 

    myfile.open("school.txt"); 
    myfile << "~School Agenda~\n\n"; 
    myfile.close(); 
} 

bir arayan -been (ben yanlış kullanmıştır sürece) -cin.get ve cin.getline işe yaramadı uzun süre bir yere cevap ver, bu benim son çare iken hayal kırıklığıyla öldürüldüm!

+0

koyun boşluklar .. Benim kayıt sistemi ile oynuyorsun ve ben nasıl kendi dosyası yazmak ve dosya içindeki tüm txt koymak için anlamaya: 'dosyam << başlık << ' '; ' –

+0

Lütfen bunu okuyun: http://stackoverflow.com/help/mcve – anukul

cevap

0

Merhaba .. ben Aptal Kafa tamamen anonim ..

örnek .. bu kendi dosya adı oluşturmak için nasıl .. Girdinizin nerede olacağını beyan etmek gerek ve txt ekleme ediyorum Dosyanın içinde. Gerek olarak

    string name,fileName; 
       fstream file; 
       cout << "Enter your name: " <<endl;   
       cin>> name; 
       fileName += ".txt"; // important to create .txt file. 
       ofstream createFile; 
       createFile.open(fileName.c_str(), ios::app); 
       createFile << name << endl;   
       if (file.fail()) { // check if file is opened successfully 
       cout << "Error Opening file ... " << endl; 
       }else{ 
       file<<name<<endl; 
+0

Bu tam olarak aradığım şey değildi ama yardımcı oluyor. Bir cümlenin uygulanmasının en iyi yolunu belirledim is_to_just_use_underscores_as_spaces. – AJaB

İlgili konular