[英]Illegal file: cannot open (/dev/tty) USACO error
I'm working on a USACO online training problem and the grader gives me the following error: 我正在处理USACO在线培训问题,并且平地机给我以下错误:
Run 2: Execution error: Your program had this runtime error: Illegal file open (/dev/tty).
运行2:执行错误:您的程序有此运行时错误:非法文件打开(/ dev / tty)。 The program ran for 0.000 CPU seconds before the error.
该程序在错误发生之前运行了0.000 CPU秒。 It used 2160 KB of memory.
它使用了2160 KB的内存。
The program works on my computer (Windows 8: Codeblocks, mingw) but not on the grader machine (Linux). 该程序可以在我的计算机上运行(Windows 8:Codeblocks,mingw),但不能在平地机上运行(Linux)。 Even weirder is that test 1 worked fine but the error appears on run 2. Based on the related USACO help page, the error is most likely due to some out-of-bound array indexing, but I'm unable to find the error.
甚至更奇怪的是,测试1可以正常运行,但该错误出现在运行2上。基于相关的USACO帮助页面,该错误最有可能是由于某些超出范围的数组索引编制而引起的,但我找不到该错误。 My input file is just two integers separated by a space, followed by a newline (in this case the input is
9 10
), and the output file looks like 我的输入文件只是两个整数,中间用一个空格隔开,后跟一个换行符(在这种情况下,输入为
9 10
),输出文件看起来像
15
16
17
18
20
21
24
26
27
Here's my code: 这是我的代码:
/*
TASK:dualpal
LANG:C
ID:tanishq1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <string.h>
void strreverse(char * str)
{
if (str)
{
char *end = str + strlen(str) - 1;
// swap the values in the two given variables
// XXX: fails when a and b refer to same memory location
# define XOR_SWAP(a,b) do\
{\
a ^= b;\
b ^= a;\
a ^= b;\
} while (0)
// walk inwards from both ends of the string,
// swapping until we get to the middle
while (str < end)
{
XOR_SWAP(*str, *end);
str++;
end--;
}
# undef XOR_SWAP
}
}
int palindromecheck(char *palstring) {
char *reversepalstring;
reversepalstring = (char *)malloc(sizeof(palstring) * sizeof(char));
strcpy(reversepalstring,palstring);
strreverse(reversepalstring);
if (strcmp(reversepalstring,palstring) == 0) {
return 1;
}
else {
return 0;
}
}
char returnstring[34];
char *baseconvert(int number,int base) {
if(number == 0 || base == 10) {
sprintf(returnstring,"%d",number);
return returnstring;
}
int returnint = (number % base) + (10 * atoi(baseconvert(number / base, base)));
sprintf(returnstring,"%d",returnint);
return returnstring;
}
int main() {
FILE *inputfile = fopen("dualpal.in","r");
FILE *outputfile = fopen("dualpal.out","w");
int n;
int s;
fscanf(inputfile,"%d %d\n",&n,&s);
int i = 1, ncounter = 0, scounter = 1;
for (i; ncounter < n; i++) {
int j = 2;
int palindromesuccesscounter = 0;
for (j; j <= 10; j++) {
int palindrome = palindromecheck(baseconvert(s + scounter, j));
if (palindrome == 1) {
palindromesuccesscounter++;
}
}
if (palindromesuccesscounter > 1) {
fprintf(outputfile,"%d\n",s + scounter);
ncounter++;
}
scounter++;
}
fclose(inputfile);
fclose(outputfile);
return 0;
}
Based on the related USACO help page, the error is most likely due to some out-of-bound array indexing
根据相关的USACO帮助页面,该错误最有可能是由于某些超出范围的数组索引
In your palindromecheck
function, you have: 在您的
palindromecheck
功能中,您具有:
int palindromecheck(char *palstring) {
char *reversepalstring;
reversepalstring = (char *)malloc(sizeof(palstring) * sizeof(char));
...
}
palstring
is a pointer, not an array, and sizeof(palstring)
will give you the size of a pointer (likely 4 or 8 bytes), not the length of the string. palstring
是一个指针,而不是数组,而sizeof(palstring)
将为您提供指针的大小(可能为4或8个字节),而不是字符串的长度。
You need to use strlen
to determine the length of the string (and add 1 to allow for the terminating '\\0'
null character). 您需要使用
strlen
来确定字符串的长度(并添加1以允许终止的'\\0'
空字符)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.