2015-06-16 46 views
0

我有一個C++程序,只要求數學,物理和體育,並保存在文件和數據文件也是這些值的平均值。到目前爲止這麼好,但我現在需要的是,可以詢問用戶是否想要檢查/可視化寫在其上的內容,以及他是否說他們想要編輯它。我認爲這是一項非常艱鉅的任務,但它也適用於我。C++讀取和編輯文件

#include <stdio.h> 

int main(void) 
{ 
    char url[]="notas.txt"; 
    float nota, 
      media=0.0; 
    FILE *arq; 

    arq = fopen(url, "w"); 
    if(arq == NULL) 
      printf("Error, it wasnt possible to open the file\n"); 
    else{ 
     printf("Maths eavluation: "); 
     scanf("%f", &nota); 
     fprintf(arq, "Matematica: %.2f\n", nota); 
     media+=nota; 

     printf("P.E evaluation: "); 
     scanf("%f", &nota); 
     fprintf(arq, " Educacao Fisica: %.2f\n", nota); 
     media+=nota; 

     printf("quemistry Eavluation: "); 
     scanf("%f", &nota); 
     fprintf(arq, " Fisico Quimica: %.2f\n", nota); 
     media+=nota; 

     media /= 3; 
     fprintf(arq, "Media final: %.2f\n", media); 
    } 
    fclose(arq); 

    printf("do u want to check the file??") 
    //from here i dont know what to du ....XP 

    return 0; 
} 

如果你想,我多翻譯一下就問好嗎?

+0

這是怎麼回事C++? – NathanOliver

+1

@NathanOliver:因爲它可以由C++編譯器編譯? –

+1

有什麼要求?讀取輸入後,程序需要做什麼? –

回答

0

這是你想要的開始。因爲這是一項任務,我已經給你留下了一些閱讀,剩下的任務。我還翻譯了許多用葡萄牙語閱讀的代碼。


#include <iostream> 
#include <fstream> 

// seu códe 
// Faça isso para ler o conteúdo do arquivo. Este não é testado 
fclose(arq); 
std::ifstream meu_arquivo; 
meu_arquivo.open(url, std::ifstream::in); 
std::cout << "Digite sim , se você quiser ver as notas: "; 
std::string resposta; 
std::cin >> resposta; 
if (resposta == "sim" && meu_arquivo.good()) { 
    // imprimir o conteúdo do arquivo. 
    std::string line; 
    while (std::getline(infile, line)) { 
     std::cout << line << std::endl; 
    }   
} else { 
    meu_arquivo.close(); 
} 

// Edição de um arquivo é deixado como um exercício . Por favor, leia 
// http://www.cplusplus.com/doc/tutorial/files/ para mais detalhes.