我如何在C中采取限制? [英] How do I take a restriction in C?
问题描述
您好,
我在C中编码。我最近创建了一个程序,该程序读取包含两行数字的文本文件。我希望第一行只有一个数字,第二行我想要多少。我不关心数字的值只是一个整数。如果第一行中的数字大于1则会打印错误并且返回0;
我使用 fscanf()
库但我无法编码。
eg
text.txt --------
9859< -only one number
29 5898 95 9594
9859 895< - 超过一个数字。打印错误!
29 5898 95 9594
这是 fscanf
代码:
fscanf(fp, %d跨度>,试验#); // 这里是fscanf()命令?我需要使用fget()吗?
int readch = 0 ;
long int filepos = 0L;
filepos = ftell(fp); // 获取文件位置
而((readch = fgetc(fp))!= EOF){
// 读取每个字符文件
如果(isalpha(readch)){
// 检查每个字符是否为字母
printf( 文件包含字母\ n); // 打印错误消息
fclose(fp); // 关闭文件
getchar(); // 等待用户按键
返回 1 ; // 退出程序
}
}
fseek(fp,filepos,SEEK_SET); // 将文件位置移回到读取数字后的位置
< span class =code-keyword> if (num< 1 || num> 1000000 )
{
// 将限制设置为整数
printf( 数字必须为1< = N< = 1.000.000,strerror (错误)); // 错误及整数
getchar(); // 等待用户按键
返回 0 ; // 返回0的int,退出程序
}
else
{
// 如果一切正常.....
int i = 0 跨度>; // 编译器创建整数int i,
int value = 0 ; // ,其值为0。
while (fscanf(fp, %d,& ; value)== 1 )
{
i ++; // 此循环将继续,直到EOF或非整数输入
}
if (i> num) // 如果第二个文件的行有更多的整数
// 比第一行中的第一个数字说的...
{
printf( 太多整数\ n 跨度>); // ...然后将打印错误。
getchar(); // 等待用户按键
返回 1 ; // 代码将返回1,退出程序
}
if (i< num)
{
// 如果文件的第二行具有较少的整数
// 第一行中的第一个数字表示...
printf( 没有足够的整数\ N跨度>); // ...然后将打印错误。
getchar();
// 等待用户按键
return 1 ; // 代码将返回1,退出程序
}
// !如果每个限制都通过,代码将会继续!
任何人都可以帮助我吗?
问候,
George
你应该完全阅读每一行(类似fread
),然后使用strtok
将其拆分为其组件。然后,您可以计算每行中的项目数,并使用atoi
将它们从字符串转换为整数。
fscanf和scanf aren解析非常灵活。很难对数据进行适当的验证。很多原因没有使用(f)scanf。
这就是说,这是一个让它适合你的技巧。这将识别第一个数字是否是唯一的数字。限制是此号码后面没有空格。如果你必须接受1234作为第一行,这将不起作用。
你也应该明白 - 因为(f)scanf的工作方式,将跳过前导空白行。 (f)scanf跳过所有领先的空格 - 包括换行符。
char peek = ' ';
if (fscanf(fp, % d%c,& value,& peek)== 2 && peek = ' \ n')
{
// < span class =code-comment>解析循环中的剩余数字。
while (fscanf(fp, %d,& value)== 1 ))
{
// 等。
}
}
很明显,代码会提取第一个数字后的字符,并检查此字符是否为换行符。
查看'c-read-fil e线由行 。关于逐行读取文本文件的最佳方法是一个长期讨论。
Hello,
I code in C. I recently created a program that read a text file that contains two lines of numbers. I want for the first line to be only one number and for the second line as many as I want. I don't care for the value of the number just to be one integer. If the numbers are greater than 1 in the first line then will print an error and will return 0;
I use the fscanf()
library but I can't code it.
eg
text.txt--------
9859 <-only one number
29 5898 95 9594
9859 895 <-more than one numbers. Print an error!
29 5898 95 9594
Here is the fscanf
code:
fscanf(fp,"%d",&num); // here goes the fscanf() command ?Do I need to use fget()?
int readch = 0;
long int filepos = 0L;
filepos = ftell ( fp); // get the file position
while ( ( readch = fgetc ( fp)) != EOF) {
// read each character of the file
if ( isalpha ( readch)) {
// check each character to see if it is a letter
printf ( "File contains letters\n"); // print error message
fclose ( fp); // close file
getchar(); // wait for the user to press a key
return 1; // exit the program
}
}
fseek ( fp, filepos, SEEK_SET); // move file position back to where it was after reading num
if(num < 1 || num > 1000000)
{
// set restrictions to integer
printf("The number must be 1 <= N <= 1.000.000",strerror(errno)); // error with the integer number
getchar(); // wait the user press a key
return 0; // returning an int of 0, exit the program
}
else
{
// if everything works.....
int i = 0; // The compiler creates the integer "int i",
int value = 0; // with the value of "0".
while ( fscanf ( fp, "%d", &value) == 1)
{
i++; // this loop will continue until EOF or non-integer input
}
if ( i > num) // If the second line of the file has more integers
// than the first number in the first line says...
{
printf ( "too many integers\n"); // ...then will "print" an error.
getchar(); // wait the user press a key
return 1; // the code will return 1, exit the program
}
if ( i < num)
{
// If the second line of the file has less integers
// than the first number in the first line says...
printf ( "not enough integers\n"); // ...then will "print" an error.
getchar();
// wait the user press a key
return 1; // the code will return 1, exit the program
}
//!here the code will continue if every restrictions was passed!
Can anyone help me?
Regards,
George
You should read each line completely (something likefread
), and then usestrtok
to split it into its components. You can then count the number of items in each line, and convert them from strings to integers usingatoi
.
fscanf and scanf aren't very flexible for parsing. It is difficult to do proper validation of data. Lots of reasons for not using (f)scanf.
That said, here's a trick to make it work for you. This will recognize whether the first number is the only number. The restriction is there can be no white space following this number. If you must accept "1234 " as the first line, this won't work.
You should also understand that - because of the way (f)scanf works, leading blank lines will be skipped. (f)scanf skips over all leading white space - including newlines.
char peek = ' '; if ( fscanf(fp, "%d%c", &value, &peek) == 2 && peek = '\n' ) { // parse remaining numbers in a loop. while ( fscanf(fp, "%d", &value) == 1) ) { // etc. } }
It should be obvious the code extracts the character following the first number and checks whether this character is a line feed.
Look at 'c-read-file-line-by-line'. It's a long discussion about the best way to read a text file line by line.
这篇关于我如何在C中采取限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!