对于文本文件infile.txt(自己建立该文件并输入内容),统计其中每个数字字符(0~9)的出现次数,将结果写
对于文本文件infile.txt(自己建立该文件并输入内容),统计其中每个数字字符(0~9)的出现次数,将结果写入文本文件outfile.txt中急急急采用c++编程,谢...
对于文本文件infile.txt(自己建立该文件并输入内容),统计其中每个数字字符(0~9)的出现次数,将结果写入文本文件outfile.txt中
急急急采用c++编程,谢谢 展开
急急急采用c++编程,谢谢 展开
展开全部
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Statistics {
public static void main(String[] args) throws IOException {
String fileIn = "[path]\\infile.txt";
String fileOut = "[path]\\outfile.txt";
int[] counter = new int[10];
BufferedReader reader = new BufferedReader(new FileReader(fileIn));
PrintWriter writer = new PrintWriter(new FileWriter(fileOut), true);
String line = reader.readLine();
while (line != null) {
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c >= '0' && c <= '9') {
counter[c - '0']++;
}
}
line = reader.readLine();
}
reader.close();
for (int i = 0; i < counter.length; i++) {
writer.println(i + "出现次数为:" + counter[i]);
}
writer.close();
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Statistics {
public static void main(String[] args) throws IOException {
String fileIn = "[path]\\infile.txt";
String fileOut = "[path]\\outfile.txt";
int[] counter = new int[10];
BufferedReader reader = new BufferedReader(new FileReader(fileIn));
PrintWriter writer = new PrintWriter(new FileWriter(fileOut), true);
String line = reader.readLine();
while (line != null) {
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c >= '0' && c <= '9') {
counter[c - '0']++;
}
}
line = reader.readLine();
}
reader.close();
for (int i = 0; i < counter.length; i++) {
writer.println(i + "出现次数为:" + counter[i]);
}
writer.close();
}
}
追问
c++编程哦、谢谢
追答
#include
#include
int main(int argc,char **argv)
{
char *in_file = "path\\infile.txt", *out_file = "path\\outfile.txt";
FILE *in = NULL, *out = NULL;
int counter[10];
memset(counter, 0, sizeof(counter));
in = fopen(in_file, "r");
if (in == NULL)
{
perror("can not open file to read!\n");
return 1;
}
while (!feof(in))
{
char c = fgetc(in);
if (c >= '0' && c <= '9')
{
counter[c - '0']++;
}
}
fclose(in);
out = fopen(out_file, "w");
if (out == NULL)
{
perror("can not open file to write!\n");
return 2;
}
for (int i = 0; i < 10; i++)
{
fprintf(out, "%d 出现的次数为:%d\n", i, counter[i]);
}
fclose(out);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询