读取txt文件中的数字列表并以C格式存储到数组中

读取txt文件中的数字列表并以C格式存储到数组中,第1张

概述我有一个整数列表,每行一个数字,并希望将这些数字存储在一个整数数组中,以便稍后在程序中使用. 例如在java中你会做这样的事情: FileReader file = new FileReader("Integers.txt");int[] integers = new int [100];int i=0;while(input.hasNext()){ integers[i] = in 我有一个整数列表,每行一个数字,并希望将这些数字存储在一个整数数组中,以便稍后在程序中使用.

例如在java中你会做这样的事情:

fileReader file = new fileReader("Integers.txt");int[] integers = new int [100];int i=0;while(input.hasNext()){   integers[i] = input.nextInt();   i++;}input.close();

如何在C中完成?

解决方法 放手一搏.如果你阅读每个函数的手册页(fopen(),scanf(),fclose())以及如何在C中分配数组,你会好得多.你还应该为此添加错误检查.例如,如果Integers.txt不存在或您没有从中读取的权限会发生什么?如果文本文件包含超过100个数字呢?

file *file = fopen("Integers.txt","r");    int integers[100];    int i=0;    int num;    while(fscanf(file,"%d",&num) > 0) {        integers[i] = num;        i++;    }    fclose(file);
总结

以上是内存溢出为你收集整理的读取txt文件中的数字列表并以C格式存储到数组中全部内容,希望文章能够帮你解决读取txt文件中的数字列表并以C格式存储到数组中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1214785.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存