求帮写一个从txt文本中读取数据并输出的函数 10C

//定义试题结构体
typedef struct node
{
int id; //试题编号
char problem[512]; //题干
char optionA[256]; //选项A
char optionB[256]; //选项B
char optionC[256]; //选项C
char optionD[256]; //选项D
char ans[4]; //正确答案
char choose[4];//学员答案
struct node*pNext; //指向下一个试题的指针
}Node;

Node *pHead = NULL; //存放试题的链表的首节点地址
Node *pTail = NULL;

//申请储存空间
void insertNode(int id, char *problem, char *optionA, char *optionB, char *optionC, char *optionD, char *ans)
{
Node *pNew = (Node *)malloc(sizeof(Node));
pNew->id = id;
strcpy(pNew->problem, problem);
strcpy(pNew->optionA, optionA);
strcpy(pNew->optionB, optionB);
strcpy(pNew->optionC, optionC);
strcpy(pNew->optionD, optionD);
strcpy(pNew->ans, ans);
pNew->pNext=NULL;

if (pHead == NULL)
{
pHead = pNew;
pTail = pHead;
}
else
{

//尾插入法
pTail->pNext = pNew;
pTail = pNew;

//头插入法
//pNew->pNext = pHead;
//pHead = pNew;

}
}

3个回答

准备一个文本文件,放在d盘,里

 面有5行

#include <stdio.h>
#include <iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;


int main()
{
ifstream inf;
inf.open("d:\\test.txt");


string sline;//每一行
string out;
char problem[512]; //题干
char optionA[256]; //选项A
char optionB[256]; //选项B
char optionC[256]; //选项C
char optionD[256]; //选项D


while(getline(inf,sline))
{
istringstream sin(sline);
sin>>problem>>problemA>>problemB>>problemC>>problemD;
cout<<problem<<" "<<problemA<<" "<<problemB<<" "<<problemC<<" "<<problemD<<"\n";

}


}
Freedomlater
Freedomlater 回复请你们大家多多姿瓷我菠菜菌: c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(19): error C2061: 语法错误: 标识符“acosf” 1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(19): error C2059: 语法错误:“;”
大约 2 年之前 回复
dabocaiqq
虾基霸回答 回复Freedomlater: 我这里成功的啊,你什么错误?
大约 2 年之前 回复
Freedomlater
Freedomlater 我怎么生成失败了
大约 2 年之前 回复

#include
#include
#include
#include
#include
using namespace std;

int main()
{
ifstream inf;
inf.open("d:\test.txt");

string sline;//每一行
string out;
char problem[512]; //题干
char optionA[256]; //选项A
char optionB[256]; //选项B
char optionC[256]; //选项C
char optionD[256]; //选项D

while(getline(inf,sline))
{
istringstream sin(sline);
sin>>problem>>problemA>>problemB>>problemC>>problemD;
cout<<problem<<" "<<problemA<<" "<<problemB<<" "<<problemC<<" "<<problemD<<"\n";

}

}

 也可以这样写:
ifstream fin(test.txt");

 if( ! fin )
 {
  cerr<<"Can not open datain1 file."<<endl;
  getchar();
  return 1;
 };

 while( ! fin.eof() )
 {
  Datalen++;
  if( Datalen > 99 )
  {
   cerr<<"Too much Data!"<<endl;
   getchar();
   return 1;
  };
  fin>>Data[Datalen];
 };

 fin.close();  
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问