有很多方法可以将拼图的各个部分拼凑在一起。找到工作的工具是战斗的 1/2。在这种情况下,strtol 会将基数 2 转换为十进制。关键是要认识到,没有理由做character input,您可以使用line-oriented input 简化您的代码,它将以准备好转换的格式提供您的数据。
以下是拼图的部分。它们有些乱序,因此您可以重新排列它们以生成最终输出文件,其中包含字符串和十进制值。您可能希望在读取文本文件之前打开输出文件,以便在读取循环期间两个文件流都可用。
看看,如果您有任何问题,请告诉我。 注意:这只是解决此问题的众多方法之一:
#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' (49) 的ASCII 值。如果您将 8 添加到其中,那么您可以进行数学计算。当您以 text 的形式从文件中读取时,您读取的是 ASCII 值,NOT 是数值 1 或 0。
为了完成您似乎正在尝试的事情,您必须将一行中的所有字符保存到缓冲区中(string,character array,我不在乎你怎么称呼它)。这是唯一的方法,(除了逐个字符转换为数字1 或0 然后执行binary addition),您必须转换'0's 和@987654339 的字符串@s 转换为十进制值。
这是一个使用从fi 读取的character-by-character 的示例。仔细阅读并理解为什么需要以这种方式完成。如果您有任何问题,请发表另一条评论。
#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