【问题标题】:writing inputs to file and reading from file in C++在 C++ 中将输入写入文件并从文件中读取
【发布时间】:2017-01-25 08:06:42
【问题描述】:

我终于能够在输入和显示 n 个学生详细信息(学生 ID#、姓名和年龄)时生成一个工作代码...虽然名称不是全名,但我想将这些输入写入文本文件,然后也从文本文件中读取。如有任何帮助,我将不胜感激:

#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;

}

【问题讨论】:

  • 你不应该“跨越溪流”。使用std::coutprintf。同样,std::cinfscanf
  • main 函数有两个参数或没有。这两个参数是:1)参数计数和2)参数字符串数组。没有合法的int main(int e) 声明。
  • 我建议烧掉你正在学习的书或停止访问教程或停止观看视频。这里的问题太多了。
  • 您创建了一个输出文件,但不写入。为了更好地参考,请查看文件流部分,尤其是文件输出部分。
  • 对不起,Thomas,违反了这条规则……但它有点奏效。我实际上结合了 5 个不同的部分,并提出了一些疯狂的想法来想出这个。文件流的这两行是什么让我的大脑僵住了

标签: c++ string io


【解决方案1】:

我认为您不知道如何通过文件读取或写入。这就是为什么你有问题。人们通常使用两种方式。通过这两个示例教程可以很好地理解这些方法。 - 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 文件。

【讨论】:

  • 用 C++ 思考,我仍然无法让我的代码工作。我想我会再挣扎一点。我想我只想将数据写入文本文件...我不想再读取数据了。
【解决方案2】:

我建议从一个写入文件的简单程序开始:

#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;
}

执行上述程序后,您应该验证文本文件是否包含正确的文本。

接下来,您可以添加其他代码,例如提示用户和获取输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    相关资源
    最近更新 更多