标准IO练习

 使用fputc和fgetc实现文件的拷贝功能

#include <myhead.h>
int main(int argc, const char *argv[])
{
    FILE *fp = fopen(argv[1], "r");
    FILE *fq = fopen( argv[2],"w");
    if(NULL == fq || NULL == fp)
    {
        perror("fopen");
        return 1;                            
    }
    while(1){
        char ch = fgetc(fp);
        if(EOF == ch){break;}
        fputc(ch, fq);
    }
    
    return 0;
}

将结构体数组的加载保存代码,把结构体数组改成链表再来一次

#include <myhead.h>
#include <string.h>

typedef struct student
{
	char name[20];
	int chinese;
	int math;
	int english;
	int physics;
	int chemical;
	int biology;
}stu;

typedef struct student_node
{
	stu student;
	struct student_node *next;
	int len;
}stu_node, *stu_node_p;

stu_node_p node_create()
{
	stu_node_p p = (stu_node_p)malloc(sizeof(stu));
	if(NULL == p)
	{
		printf("申请失败");
		return NULL;
	}
	return p;

}

int main(int argc, const char *argv[])
{
	stu_node_p H = (stu_node_p)malloc(sizeof(stu_node));
	if(NULL == H)
	{
		printf("头结点创建失败");
		return 0;
	}
	H -> len = 0;
	H -> next = NULL;

	stu srr[5] = {{"李",1,1,1,1,1,1},
		{"王",2,2,2,2,2,2},
		{"刘",3,3,3,3,3,3},
		{"张",4,4,4,4,4,4},
		{"胡",5,5,5,5,5,5}};
	do
	{
		stu_node_p p = node_create();
		p -> student = srr[H -> len];
		p -> next = H -> next;
		H -> next = p;
		H -> len++;
	}while(H -> len < 5);

	
	FILE *fp = fopen(argv[1], "w");
	if(NULL == fp)
	{
		perror("fopen");
		return 1;
	}

	stu_node_p p = H -> next;
	do
	{
		fprintf(fp, "%s %d %d %d %d %d %d\n",
				p -> student.name,
				p -> student.math,
				p -> student.chinese,
				p -> student.english,
				p -> student.physics, 
				p -> student.chemical,
				p -> student.biology);
		p = p -> next;
	}while(p != NULL);

	p = H -> next;
	for(int i = 0; i < H -> len; i++)
	{
		printf("%s %d %d %d %d %d %d\n",
				p -> student.name,
				p -> student.math,
				p -> student.chinese,
				p -> student.english,
				p -> student.physics, 
				p -> student.chemical,
				p -> student.biology);
		p = p -> next;
	}
	fclose(fp);//调用完需要关闭,方便其他文件流调用文件
#if 1
//清空后从新读取
	p = H -> next;
	do
	{
		memset(&p -> student, 0, sizeof(p -> student));
		p = p -> next;
	}while(p != NULL);

	FILE *fq = fopen(argv[1], "r");
	if(NULL == fq)
	{
		perror("open");
		return 1;
	}
	p = H -> next;
	do
	{
		fscanf(fq, "%s %d %d %d %d %d %d\n",
				p -> student.name,
				&p -> student.math,
				&p -> student.chinese,
				&p -> student.english,
				&p -> student.physics, 
				&p -> student.chemical,
				&p -> student.biology);
		p = p -> next;
	}while(p != NULL);

	p = H -> next;
	for(int i = 0; i < H -> len; i++)
	{
		printf("%s %d %d %d %d %d %d\n",
				p -> student.name,
				p -> student.math,
				p -> student.chinese,
				p -> student.english,
				p -> student.physics, 
				p -> student.chemical,
				p -> student.biology);
		p = p -> next;
	}
#endif

	fclose(fq);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较典型的IO操作题目,需要使用Java的随机数生成、文件IO读写等知识点。下面是一种可能的实现方式: ```java import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Random; import java.util.Scanner; public class RandomNumbers { public static void main(String[] args) throws FileNotFoundException { // 生成随机数并写入文件 PrintWriter writer = new PrintWriter(new File("random_numbers.txt")); Random random = new Random(); for (int i = 0; i < 1000; i++) { int num = random.nextInt(950) + 50; writer.printf("%d ", num); if ((i + 1) % 10 == 0) { writer.println(); } } writer.close(); // 读取文件并累计求和 Scanner scanner = new Scanner(new File("random_numbers.txt")); int sum = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); Scanner lineScanner = new Scanner(line); int num = lineScanner.nextInt(); sum += num; lineScanner.close(); } scanner.close(); // 输出结果 System.out.printf("文件中每行的首个数字累计求和为:%d\n", sum); } } ``` 这里我们使用了Java标准库中的Random类来生成随机数,使用PrintWriter类将随机数输出到文件中,使用Scanner类从文件中读取数据并累计求和。注意,这里的实现中,每行输出10个数字,数字之间用空格分隔,每行以换行符结束。在读取文件时,我们先读取整行数据,再使用Scanner类分割每一行的数字,并取出每行的首个数字进行累加求和。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值