Apparently I'm having some issue reading a file, line by line, or character by character, because each row of my file is terminated by a newline character. I think the issue is related to those newline characters because when I'm trying to read the file, using or fgetc() or fgets(), when I attempt to print the result on stdout I'm given... nothing.
显然,我对逐行读取文件或逐行读取字符有一些问题,因为文件的每一行都以换行符结束。我认为这个问题与这些换行字符有关,因为当我尝试读取文件时,使用或fgetc()或fgets(),当我尝试在stdout上打印结果时,我得到了……什么都没有。
Example of first file that gives me the issue:
给我的第一个文件的例子:
12345678
12849499
47484900
I tried with another file, such as
我尝试了另一个文件,比如。
123456596945869498
And the output on stdout, parsing the file using fgetc() or fgets(), is what I expect: the content of the file.
而stdout的输出,使用fgetc()或fgets()解析文件,是我所期望的:文件的内容。
Now, the aim of the read from file is to store the content of the file in a matrix at pointers. I tried in many ways to bypass those newlines. I tried like this:
现在,从文件读取的目的是将文件的内容存储在一个指针上的矩阵中。我尝试了很多方法来绕过这些新思路。我试着像这样:
i = 0;
while((c = fgetc(f)) != EOF){
if(c != '\n')
p[i][j++] = c; /*Incrementing j by one ad each iteration */
if(c == '\n')
i++; /*If c == '\n', skip to the next row of the matrix */
}
Where I is the row index and j is the column index. I even tried with fgets, like this, as suggested by a member of this forum:
其中I是行索引,j是列索引。我甚至尝试过fgets,就像这个论坛的一位成员建议的那样:
while((fgets(line, col,f))!= NULL){
p[i++] = strdup(line);
}
Can anybody help me figuring out how to solve this issue?
谁能帮我解决这个问题吗?
This is the main of the my program:
这是我项目的主要内容:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define K 100
int main() {
FILE *f;
f = fopen("out_test6_1.txt", "r");
int row = 0;
int col = 0;
char c;
// Evaluating number of rows and columns of the new matrix,
// parsing the file in search of newlines chars
while((c = fgetc(f)) != EOF ) {
if(c != '\n' && row == 0)
col++;
else if(c == '\n')
row++;
}
printf("rows %d\n", row);
printf("columns %d\n", col);
int i, j;
char**p = malloc(row*sizeof(char*));
for(i = 0; i < row; i++) {
p[i] = malloc(col*sizeof(char));
}
i = 0;
while((c = fgetc(f)) != EOF) {
if(c != '\n')
p[i][j++] = c;
if(c == '\n')
i++;
}
for(i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
printf("%c", p[i][j]);
}
}
fclose(f);
return 0;
}
1 个解决方案
#1
0
Code reads the file to find the rows and columns yet fails to rewind before the 2nd pass.
代码读取文件以查找行和列,但在第2遍前未能回滚。
i = 0;
rewind(f); // add
while((c = fgetc(f)) != EOF) {
Some other problems noted:
其他一些问题指出:
// To properly distinguish all characters from EOF
// char c;
int c;
I'd expect code to find the maximum column width and use that later.
我希望代码能够找到最大的列宽度并在以后使用。
size_t max_col = 0;
while((c = fgetc(f)) != EOF ) {
col++;
if (c == '\n') {
if (col > max_col) {
max_col = col;
col = 0;
}
row++;
}
}
if (col > max_col) {
max_col = col;
}
...
// p[i] = malloc(col*sizeof(char));
p[i] = malloc(sizeof *(p[i]) * max_col);
#1
0
Code reads the file to find the rows and columns yet fails to rewind before the 2nd pass.
代码读取文件以查找行和列,但在第2遍前未能回滚。
i = 0;
rewind(f); // add
while((c = fgetc(f)) != EOF) {
Some other problems noted:
其他一些问题指出:
// To properly distinguish all characters from EOF
// char c;
int c;
I'd expect code to find the maximum column width and use that later.
我希望代码能够找到最大的列宽度并在以后使用。
size_t max_col = 0;
while((c = fgetc(f)) != EOF ) {
col++;
if (c == '\n') {
if (col > max_col) {
max_col = col;
col = 0;
}
row++;
}
}
if (col > max_col) {
max_col = col;
}
...
// p[i] = malloc(col*sizeof(char));
p[i] = malloc(sizeof *(p[i]) * max_col);