如何在C ++中读取文件时转到下一行 [英] How to go to the next line while reading a file in C++

查看:471
本文介绍了如何在C ++中读取文件时转到下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决项目欧拉的第13个问题。问题是计算出以下一百个50位数字之和的前十位数字。,我将数字复制到一个文本文件中并读取每个数字然后将其添加到数组中的元素,我有解决了它,但是为了只读取前十位数字,我必须跳过接下来的40位数而不加入它们。

  for (i =  0 ; i<  100 ; i ++)
{
for (j = 0 ; j< 50 ; j ++)
{
if (file>> digit)
{
if (j> 9
继续;
numbers [i] * = 10 ;
numbers [i] + =(digit - ' 0');
}
}

但是,当然,用大文件做这样的事情将是一个大问题所以任何建议更好的解决方案,如下一行或其他什么否则?





谢谢,Samuel。

解决方案

使用fseek( )以便随机访问每一行,但考虑到只有当打开文件为二进制文件时才能正常工作(删除文本\ n转换)。

二进制文件的文件布局如下:

<预郎= 文本> 37107287533902102798797998220837590246510135740250\r\\\

46376937677490009712648124896970078050417018260538\r\\\

74324986199524741059474233309513058123726617309629\r\\\

91942213363574161572522430563301811072406154908250\ r $ \\ n
23067588207539346171171980310421047513778063246676 \\\\ n
....
53503534226472524250874054075591789781264330331690\\ n



指每个数字星ts at

(n-  1 )*  52  



其中 n 是行号,最后两个字节是\r& \ n。

如果 d 是您想在线 n 上阅读的数字,那么:

数字位于((n-1)* 52)+(50-1-d)



此示例显示访问文件:

  #include    <   stdio.h  >  
#include < stdlib .h >
#include < string.h > ;

int main( int argc , char * argv [])
{
FILE * fp;

fp = fopen( BigNums.txt rb);

if (!fp)
{
printf( 找不到文件!\ n);
返回 - 1 ;
}

for (;;)
{
char str [ 128 ];
char 行[ 52 ];

printf( 插入数字行[1-10]:) ;
scanf( %s,str);
int n = atoi(str);
if (n< 0
断裂;
if (n< 1 || n> 100)
continue ;

printf( 插入数字[1-50]:);
scanf( %s,str);
int digit = atoi(str);
如果(数字< 1 ||数字> 50)
继续;

fseek(fp,(n- 1 )* 52 ,SEEK_SET) ;
fread(line, sizeof (line), 1 ,fp);

printf( 行[%50.50s]数字[%c],前10个字符[%10.10s] \ n,行,行[ 49 - (digit- 1 )],line);

}

return 0 ;
}



我选择每次读取整数并使用fread()访问内存中的单个数字因为fseek()是一个重函数而不是经常使用它是一个好主意。



如果你想按顺序读取整行,你可以使用fgets()。使用它你不能回去。



P.S.假设文件具有指定的格式和条目数,样本未检查文件和数据格式。


Hi, I was working on solving the 13th problem of project euler. the problem is "Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.", I copied the number into a text file and read each digit then add it to an element in an array and I have solved it, But to read only the first ten digits, I had to skip the next 40 digits by reading them without adding.

for (i = 0; i < 100; i++)
{
        for (j = 0; j < 50; j++)
        {
            if (file >> digit)
            {
                if (j > 9)
                    continue;
                numbers[i] *= 10;
                numbers[i] += (digit - '0');
            }
}

But, of course, doing such thing with a big file will be a big problem so any suggestions for a better solution such as going to the next line or something else ?


Thanks, Samuel.

解决方案

Use fseek() to for random access to each line, but consider that it works well only when opening the file as binary (remove text \n translation).
In binary your file layout is as follows:

37107287533902102798797998220837590246510135740250\r\n
46376937677490009712648124896970078050417018260538\r\n
74324986199524741059474233309513058123726617309629\r\n
91942213363574161572522430563301811072406154908250\r\n
23067588207539346171171980310421047513778063246676\r\n
....
53503534226472524250874054075591789781264330331690\r\n


Means that each number starts at

(n-1)*52


Where n is the line number the last two bytes are \r & \n.
If d is the digit you want read on line n then:

digit is at ((n-1)*52)+(50-1-d)


This sample shows as to access file:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
	FILE *fp;

	fp = fopen("BigNums.txt", "rb");

	if (!fp)
	{
		printf("File not found!\n");
		return -1;
	}

	for(;;)
	{
		char str[128];
		char line[52];

		printf("Insert number line [1-10]: ");
		scanf ("%s", str);
		int n = atoi(str);
		if (n < 0)
			break;
		if (n<1 || n>100)
			continue;

		printf("Insert digit [1-50]: ");
		scanf ("%s", str);
		int digit = atoi(str);
		if (digit<1 || digit>50)
			continue;

		fseek(fp, (n-1)*52, SEEK_SET);
		fread(line, sizeof(line), 1, fp);

		printf("Line [%50.50s] digit [%c], first 10 chars [%10.10s]\n", line, line[49-(digit-1)], line);

	}

	return 0;
}


I choosed to read a whole number each time and access single digits in memory using fread() because fseek() is an heavy function and is not a good idea to use it very often.

If you want read full lines sequentially you can use fgets(). Using it you cannot move back.

P.S. The sample miss checks on file and data format assuming that the file has the specified format and number of entries.


这篇关于如何在C ++中读取文件时转到下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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