2016-09-17 214 views
0

我終於能夠輸入和顯示n個學生詳細信息(學生ID#,姓名和年齡),儘管名稱不是全名,我想將這些輸入寫入文本文件,然後從文本文件中讀取。我感謝所有幫助,請:將輸入寫入文件並從C++中讀取文件

#include <stdio.h> 
#include <iostream> 
#include <cstdlib> 
#include <cstdio> 
#include <fstream> 

using namespace std; 

struct student 

{ 
    int sno, sage; 
    char sname[100]; 
}; 

    int main(int e, char* argv[]) 
{ 

    struct student s[e]; 
    int i; 

    ofstream outfile; 
    outfile.open("info.txt"); 

printf("How many entries are you making?: "); 
scanf("%d",&e); 

printf(" \n"); 


printf("Please enter Student Information:\n"); 

     for(i=0;i<e;++i) 
{ 
    s[i].sno=i+1; 

    printf("\nEnter Information for student %d\n",s[i]); 
    printf("================================\n"); 


    cout<<"\nEnter (4 digits) student ID No: "; 
    cin>>s[i].sno; 

    cout<<"\nEnter student's name: "; 
    cin>>s[i].sname; 


    cout<<"\nAge of student: "; 
    cin>>s[i].sage; 

    printf("\n"); 

//If i do this, I get only first set of data 
outfile <<s[i].sno<<" "<<s[i].sname<<" "<<s[i].sage<<endl; 

/*I am trying to get data into txt file using the loop below but it looks odd and it collects some of the first set of data, and completes it like this: 1212 kop 23 
1627953384 1629646589*/ 

    /*for(i=0;i<e;++i) 
    { 
     outfile <<s[i].sno<<" "<<s[i].sname<<" "<<s[i].sage<<endl; 
    }*/ 

    outfile.close(); 
} 

printf("Displaying information of student(s):\n"); 
printf("==================================\n"); 

for(i=0;i<e;++i) 
{ 
    printf("\nInformation for Student %d:\n",i+1); 

    cout<<"\nStudent ID:"<<s[i].sno; 

    cout<<"\nStudent name: "<<s[i].sname; 

    cout<<"\nAge of student: "<<s[i].sage; 

    printf("\n\n"); 

} 

return 0; 

} 
+0

你不應該「穿過溪流」。使用'std :: cout'或'printf'。同樣,'std :: cin'或'fscanf'。 –

+0

'main'函數有兩個參數或沒有。這兩個參數是:1)參數計數和2)參數字符串數組。沒有合法的'int main(int e)'聲明。 –

+0

我建議燒你正在學習的書,或停止訪問turorial或停止觀看視頻。這裏的問題太多了。 –

回答

0

我建議從一個簡單的程序寫入一個文件:

#include <fstream> 
#include <iostream> 
#include <cstdlib> 

int main(void) 
{ 
    std::ofstream output_file("text.txt"); 
    std::cout << "Writing to output file.\n"; 
    output_file << "This is my text.\n"; 
    output_file.flush(); 
    std::cout << "Finished writing to file.\n"; 
    output_file.close(); 

    std::cout << "Paused. Press Enter to continue.\n"; 
    std::cin.ignore(100000, '\n'); 
    return EXIT_SUCCESS; 
} 

執行上述程序後,你應該確認該文本文件包含正確的文字。

接下來,您可以添加其他代碼,例如提示用戶並獲取輸入。

0

我認爲你不知道如何通過文件讀取或寫入。這就是爲什麼你有問題。人們通常使用兩種方式。仔細閱讀這兩個示例教程,以便更好地理解這些方法。 - c methods - c++ methods

這是你的代碼,我已經修改了讀取和寫入從使用這兩種方法的文件 -

Given Input file - student.txt. //first line No. of input 
2 
1234 manish  34 
4321 mukesh  43 

Code 
#include <iostream> 
#include <cstdio> 
#include <fstream> 

using namespace std; 

struct student 
{ 
int sno, sage; 
char sname[50]; 
}; 

//Read data using C Method 
void ReadAndWriteCMethod(){ 
    student s[100]; 


    FILE* f; 
    f = fopen("student.txt","r"); 
    int N; 
    fscanf(f,"%d",&N); 

    **//Reading from file** 
    for(int i=0; i<N; i++){ 
     fscanf(f,"%d %s %d",&s[i].sno, &s[i].sname, &s[i].sage); 
    } 
    fclose(f); 

    **//Writing over file** 
    f = fopen("wstudent1.txt","w+"); 
    for(int i=0; i<N; i++){ 
     cout<<s[i].sno<<" "<<s[i].sname<<" "<<s[i].sage<<endl; 
     fprintf(f,"%d %s %d\n",s[i].sno,s[i].sname,s[i].sage); 
    } 
    fclose(f); 
} 

//Reading and writing using C++ 
void ReadAndWriteCPlusPlusMethod(){ 
    student s[100]; 

    **// Reading data** 
    std::ifstream fin("student.txt"); 

    char s2[100]; 
    int N; fin>>N; 
    for(int i=0; i<N; i++){ 
    fin>>s[i].sno>>s[i].sname>>s[i].sage; 
    cout<<s[i].sno<<" "<<s[i].sname<<" "<<s[i].sage<<endl; 
    } 
    fin.close(); 

    **//Writing data** 
    std::ofstream fout("wstudent2.txt"); 
    for(int i=0; i<N; i++){ 
    fout<<s[i].sno<<" "<<s[i].sname<<" "<<s[i].sage<<endl; 
    }  
    fout.close(); 
} 

int main() 
{ 
    ReadAndWriteCMethod(); 
    ReadAndWriteCPlusPlusMethod(); 
} 

運行,這將產生兩個文件wstudent1.txt和wstudent2後。 txt文件。

+0

在C++中思考,我仍然無法讓我的代碼工作。我想我會更努力一點。我想我只是想將數據寫入文本文件...我不想再讀取數據。 – McNeal

相關問題