C IO结果。调整代码
我需要一些人帮我做作业。这就是我到目前为止所做的:C IO结果。调整代码,c,arrays,io,C,Arrays,Io,我需要一些人帮我做作业。这就是我到目前为止所做的: #include <stdio.h> #include <stdlib.h> #define MAXN 100 int main(){ int ch = 0; FILE *fi = NULL; FILE *fo = NULL; int numo = 0; int numi = 0; int nump = 0; fo = fopen("OutputFile.txt", "w
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int main(){
int ch = 0;
FILE *fi = NULL;
FILE *fo = NULL;
int numo = 0;
int numi = 0;
int nump = 0;
fo = fopen("OutputFile.txt", "w+");
if (!fo) {
perror ("Error while opening the file.\n");
exit (EXIT_FAILURE);
}
fi = fopen("InputFile.txt","r");
if(!fi){
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("\n The contents of %s file are :\n", "InputFile.txt");
while( ( ch = fgetc(fi) ) != EOF )
printf("%c",ch);
numi = ch;
numo = numi + 8;
fprintf (fo, " %d\n", numo);
if (fo) fclose (fo);
return 0;
}
当我打开OutputFile.txt时,该行显示为7。因此,出于某种原因,CH=-1(我希望它等于10010110),我不确定-1来自何处。有很多方法可以将拼图的各个部分组合在一起。找到工作的工具是这场战斗的一半。在这种情况下,
strtol
将把基数2转换为十进制。关键是要认识到,没有理由进行字符输入
,您可以使用面向行的输入
简化代码,它将以可供转换的格式提供数据
下面是拼图的各个部分。它们的顺序有些混乱,因此您可以重新排列它们,以生成包含字符串和十进制值的最终输出文件。您可能希望在读取文本文件之前打开输出文件,以便在读取循环期间两个文件流都可用
看一看,如果你有任何问题,请告诉我注意:这只是解决此问题的众多方法之一:
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int main () {
char file_input[25] = { 0 }; /* always initialize all variables */
char file_output[25] = { 0 };
FILE *fi = NULL;
FILE *fo = NULL;
int integers[MAXN] = { 0 };
int i = 0;
int num = 0;
printf ("\n Please enter the input filename: ");
while (scanf ("%[^\n]%*c", file_input) != 1)
fprintf (stderr, "error: read failed for 'file_input', try again\n filename: ");
fi = fopen (file_input, "r"); /* open input file and validate */
if (!fi) {
perror ("Error while opening the file.\n");
exit (EXIT_FAILURE);
}
printf ("\n The contents of file '%s' are :\n\n", file_input);
char *line = NULL; /* NULL forces getline to allocate */
size_t n = 0; /* max chars to read (0 - no limit */
ssize_t nchr = 0; /* number of chars actually read */
while ((nchr = getline (&line, &n, fi)) != -1) {
if (line[nchr - 1] == '\n')
line[--nchr] = 0; /* strip newline from end of line */
integers[i] = strtol (line, NULL, 2); /* convert to decimal */
printf (" %s -> %d\n", line, integers[i]);
if (i == MAXN - 1) { /* check MAXN limit not exceeded */
fprintf (stderr, "error: input lines exceed %d\n", MAXN);
exit (EXIT_FAILURE);
}
i++;
}
if (line) free(line); /* free memory allocated by getline */
if (fi) fclose (fi); /* close file stream when done */
num = i; /* save number of elements in array */
printf ("\n Conversion complete, output filename: ");
while (scanf ("%[^\n]%*c", file_output) != 1)
fprintf (stderr, "error: read failed for 'file_output', try again\n filename: ");
fo = fopen (file_output, "w+"); /* open output file & validate */
if (!fo) {
perror ("Error while opening the file.\n");
exit (EXIT_FAILURE);
}
for (i = 0; i < num; i++) /* write integers to output file */
fprintf (fo, " %d\n", integers[i]);
if (fo) fclose (fo);
return 0;
}
逐字阅读 虽然这不是处理读取文件的最简单方法,但它没有任何问题。但是,您有逻辑问题。具体来说,您读取(并指定为整数)
numi=ch代码>然后分配numo=numi+8
写入输出文件。这导致将8
添加到'0'
(48)或'1'
(49)的ASCII值中。如果你加上8,那么你就可以算数了。当您以文本形式从文件中读取时,您正在读取ASCII值,非数值1
或0
为了完成您似乎要尝试的操作,您必须将一行中的所有字符保存到缓冲区中(一个字符串
,一个字符数组
,我不管您如何称呼它)。这是唯一的方法,(除了逐字符转换为数字1
或0
,然后执行二进制加法
),您必须将'0'
和'1'
的字符串转换为十进制值
下面是一个使用从fi
读取的字符的示例。仔细阅读并理解为什么需要这样做。如果您有问题,请发表另一条评论
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int main () {
int ch = 0;
FILE *fi = NULL;
FILE *fo = NULL;
// int numo = 0;
// int numi = 0;
// int nump = 0;
char buffer[MAXN] = { 0 }; /* buffer to hold each line */
int idx = 0; /* index for buffer */
fo = fopen ("OutputFile.txt", "w+"); /* open output file & validate */
if (!fo) {
perror ("Error while opening the file.\n");
exit (EXIT_FAILURE);
}
fi = fopen ("InputFile.txt", "r"); /* open input file & validate */
if (!fi) {
perror ("Error while opening the file.\n");
exit (EXIT_FAILURE);
}
printf ("\n The contents of %s file are :\n\n", "InputFile.txt");
fprintf (fo, " binary decimal\n"); /* header for output file */
while (1) /* loop and test for both '\n' and EOF (-1) to parse file */
{
// printf ("%c", ch); /* we will store each ch in line in buffer */
if ((ch = fgetc (fi)) == '\n' || ch == EOF)
{
if (ch == EOF && idx == 0) /* if EOF (-1) & buffer empty exit loop */
break;
buffer[idx] = 0; /* null-terminate buffer (same as '\0' ) */
idx = 0; /* reset index for next line & continue */
/* write original value & conversion to fo */
fprintf (fo, " %s => %ld\n", buffer, strtol (buffer, NULL, 2));
/* write fi contents to stdout (indented) */
printf (" %s\n", buffer);
}
else
{
buffer[idx++] = ch; /* assign ch to buffer, then increment idx */
}
/* This makes no sense. You are reading a character '0' or '1' from fi,
the unsigned integer value is either the ASCII value '0', which is
decimal 48 (or hex 0x30), or the ASCII value '1', decimal 49/0x31.
If you add 8 and write to 'fo' with '%d' you will get a 16-digit
string of a combination of '56' & '57', e.g. 56575756....
numi = ch;
numo = numi + 8;
*/
}
if (fi) /* close both input and output file streams */
fclose (fi);
if (fo)
fclose (fo);
return 0;
}
OutputFile.txt:
$ ./bin/arrayhelp
Please enter the input filename: dat/binin.txt
The contents of file 'dat/binin.txt' are :
01000101 -> 69
11010110 -> 214
11101110 -> 238
Conversion complete, output filename: dat/binout.txt
$ cat dat/binout.txt
69
214
238
$ ./bin/arrayhelp2
The contents of InputFile.txt file are :
01000101
11010110
11101110
$ cat OutputFile.txt
binary decimal
01000101 => 69
11010110 => 214
11101110 => 238
首先,您不知道文件的长度,因此需要动态分配内存(malloc()
,realloc()
,free()
)。您将动态分配2D数组的内存(二进制数上的指针数组)。完成计算后,使用fprinf()
将结果保存到文件中。也许您应该节省一些时间和内存,在阅读整行内容后直接进行计算。希望这能对你有所帮助。我放弃了数组的想法,在OutputFile.txt中有一些东西要打印。更新了原始帖子中的代码。谢谢。我会看看我是否能找到答案,并在稍后发布结果:将原始帖子与我现在所在的位置进行对比。你的帖子对理解这段代码有很大的帮助,尽管我对ssize\t有一些问题,并放弃了数组的想法。。。我现在处于代码的最后一位,我只是不确定为什么当我试图让它保存InputFile.txt中的数字时-1会被保存为CH。请看我题为“逐字符读取”的添加内容保存到CH的-1
是EOF
的值…numo=numi+8是我在那里检查的一些骗局代码的哪些部分正在工作。(我很奇怪,但它确实让我知道程序正在正确地读取文件,因为它确实正确地显示了inputfiles编号,并且还正确地将编号保存到outputfile。):D代码现在可以工作了,非常感谢您的解释!
$ cat OutputFile.txt
binary decimal
01000101 => 69
11010110 => 214
11101110 => 238