按字符逐行读取文件中的行,字 [英] Reading file line by line, character by character

查看:164
本文介绍了按字符逐行读取文件中的行,字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数字,每行未知lenght的,用空格分隔的文件。该号码可以是任何尺寸的有多达500个数字,所以我需要由字符阅读它字符,并将其存储到一个整数数组。我想是这样的:

I have a file with two numbers of unknown lenght per line, separated by a white space. The numbers can be of any size have up to 500 digits, so i need to read it character by character and store it into an array of integers. I tried something like this:

while(fgets(line, sizeof line, fp) != NULL) { // assuming buff handles the line lenght
//read each character until ''
   // store it
//read each character until '\n'
  // store it
}       

我试着用龟etc阅读,直到''和'\\ n',但它没有工作。任何建议将AP preciated。

I tried using fgetc to read until ' ' and '\n' but it didnt work. Any suggestions will be appreciated.

推荐答案

如果你把长双格式,相信可以应付几乎任何合理的大小,那在你身边不必担心处理线的长度。

If you take long double format, I believe you can deal with almost any reasonable "size", beside that you don't have to worry about "handling line length".

 long double num1, num2;

 while (fscanf(fp, "%Lf %Lf\n", &num1, &num2) == 2){
    printf("%Lf\t%Lf\n", num1, num2);
 //copy the numbers into whichever appropriate array
 }

这篇关于按字符逐行读取文件中的行,字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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