如何从文件中读取int和char到数组中 [英] How do I read int and char from a file into an array

查看:145
本文介绍了如何从文件中读取int和char到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件的值是这样的

BECDCBAADEBACBAEDDBEDCBAAECDCB

1234 BECDXACDXAXDXEDBXCABCDEXX



第一行是正确答案并且要与第二个中的答案进行比较(由于测试原因,只有一行),整数是id。



我有什么试过:



The file has values like this
BECDCBAADEBACBAEDDBEDCBAAECDCB
1234 BECDXACDXAXDXEDBXCABCDEXX

The first line are right answers and are to be compared to the answers in the second (only has one line because of testing reasons) and the integer is the id.

What I have tried:

#include <iostream>
#include <fstream>
using namespace std;
int main(){
    char right[30],r;
    char n=0,m=0;
    char answers[30],a;
    int id[30],i=0;


    ifstream infile;
    infile.open("exam.txt");

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

    if (!infile.is_open())
        cout<<"Error opening file. Aborting....";

    if (!outfile.is_open())
        cout<<"Error opening file. Aborting....";



    infile>>r;
    while (!infile.eof()&& n<30 && i<30){
        right [n] = r;
        infile>>id[i];
        cout<<right [n];
        cout<<id[i];
        n++;
        m++;
    }

推荐答案

以下程序(在C而不是C ++中)读取单个字符 - 我希望你能够转换到C ++。它相当长,但C ++版本(包含所有内置的类和方法)可能更短。



The following program (in C not C++) reads single characters - I hope you will be able to convert to C++. It's rather long but the C++ version (with all the built in classes and methods) may be shorter.

#include <stdio.h>
#include <ctype.h>

#define CR				13
#define LF				10
#define NUMQUESTIONS	30

typedef struct
{
	char standard[NUMQUESTIONS];
	int candidatenum;
	char ans[NUMQUESTIONS * 2 + 1];
	char eval[NUMQUESTIONS * 7 + 15];
}
EXAM, *PEXAM;

int readstandard(PEXAM pex, FILE *fp);
int processpaper(PEXAM pex, FILE *fp);

char *infile = "exam.txt", *outfile = "report.txt";


void main()
{
	FILE *fpin, *fpout;
	static EXAM ex;
	
	fpin = fopen(infile, "rb");
	fpout = fopen(outfile, "w");
	
	if(fpin == NULL || fpout == NULL)
	{
		goto eofunction;
	}
	
	readstandard(&ex, fpin);
	while(processpaper(&ex, fpin))
	{
		fprintf(fpout, "Candidate: %d\nAnswers: %s\nEvaluation: %s\n\n", ex.candidatenum, ex.ans, ex.eval);
	}
	
	eofunction:
	if(fpin == NULL)
	{
		printf("Could not open input file: %s\n", infile);
	}
	else
	{
		fclose(fpin);
	}

	if(fpout == NULL)
	{
		printf("Could not open output file: %s\n", outfile);
	}
	else
	{
		fclose(fpout);
	}
	
}

int readstandard(PEXAM pex, FILE *fp)
{
	char c;
	int i = 0;
	
	do
	{
		fread(&c, 1, 1, fp);
		if(i < NUMQUESTIONS && isupper(c))
		{
			pex->standard[i++] = c;
		}
	}
	while(c != LF);  
	
	return i;
}

int processpaper(PEXAM pex, FILE *fp)
{
	char c = 0;
	int i = 0, written = 0, tot = 0, score;
	
	pex->candidatenum = 0;
	do
	{
		if(fread(&c, 1, 1, fp) > 0)
		{
			if(isdigit(c))
			{
				pex->candidatenum = pex->candidatenum * 10 + (c - '0');
			}
			
			if(i < NUMQUESTIONS && isupper(c))
			{
				sprintf(pex->ans + i * 2, "%c ", c);
				tot += score = c == 'X' ? 0 : (c == pex->standard[i] ? 2 : -1);
				written += sprintf(pex -> eval + written, "%d. %d ", i + 1, score);
				i++;
			}
		}
	}
	while(c != LF && c);  
	if(i)
	{
		sprintf(pex -> eval + written, "Total: %d", tot);
	}
	
	return i;
}





示例输入文件:



Sample input file:

引用:

BECDCBAADEBACBAEDDBEDCBAAECDCB

1234 BEABCDXACDXAXDXEDBXCAXEBDXDEXX

117 ACBEXXXACDXAXDXEDBXCABCXBAEDDE

23456 BECDXACDXBEDCBAAEAXDXEDBXCABCX

BECDCBAADEBACBAEDDBEDCBAAECDCB
1234 BEABCDXACDXAXDXEDBXCAXEBDXDEXX
117 ACBEXXXACDXAXDXEDBXCABCXBAEDDE
23456 BECDXACDXBEDCBAAEAXDXEDBXCABCX


这篇关于如何从文件中读取int和char到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆