gpt4 book ai didi

c - IO 结果。调整代码

转载 作者:行者123 更新时间:2023-11-30 19:44:47 28 4
gpt4 key购买 nike

我需要一些作业帮助。这是我到目前为止所拥有的:

#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;
}

编辑3.废弃了数组的想法,因为它给我带来的麻烦比成功还要多。我将 inputfile.txt 中的列表减少到只有一行。数学很简单,所以我可以看到我在做什么以及哪里出了问题。我大部分情况下一切正常,只是有点小故障。

首先,程序将很好地读取文件并将其显示在程序中。问题出现在该点之后以及结果保存到 OutputFile.txt 中的点。根据我使用的 %(%s、%i、%d、%c),OutputFile.txt 中的结果是 -1、一个字符或更长的数字列表。

如何从InputFile.txt中获取号码并将其保存到numi?

这就是它的样子

The contents of InputFile.txt are: 10010110
numo=ch+8
Result (Numo) save to OutputFile.txt

当我打开 OutputFile.txt 时,该行显示为 7。因此出于某种原因 CH = -1(我希望它等于 10010110)并且我不确定 -1 来自哪里。

最佳答案

有很多方法可以将拼图的各个部分拼凑在一起。找到适合工作的工具就成功了 1/2。在这种情况下,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;
}

使用/输出:

$ ./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
<小时/>

逐字阅读

虽然这不是处理读取文件的最简单方法,但它没有任何问题。但是,你有逻辑问题。具体来说,您读取(并分配为整数)numi = ch;,然后分配numo = numi + 8;以写入输出文件。这会导致将 8 添加到 '0' (48) 或 '1 的 ASCII 值' (49)。如果你加上8,那么你就可以计算了。当您从文件中读取文本时,您读取的是ASCII值,不是数值10

为了完成您似乎正在尝试的事情,您必须将一行中的所有字符保存到缓冲区(一个字符串,一个字符数组,我不'不在乎你怎么调用它)。这是唯一的方法(不逐字符转换为数字 10,然后执行二进制加法 ),您必须将 '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;
}

输出到标准输出:

$ ./bin/arrayhelp2

The contents of InputFile.txt file are :

01000101
11010110
11101110

输出文件.txt:

$ cat OutputFile.txt
binary decimal
01000101 => 69
11010110 => 214
11101110 => 238

关于c - IO 结果。调整代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27347585/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com