从文件中读取字符串,而不是将它们保存到矩阵中

时间:2015-10-18 13:48:29

标签: c file

我一直在尝试从非格式化的.txt文件中读取随机字符,如下所示:

sadfhjk
xcvbnm
dfghjkl

然后将它们保存到矩阵然后打印到另一个文件上,但我不断得到这样的结果,我无法理解为什么我在输入后得到那些随机字符串:

sadfhjk
xþcvbnm
dfgwhjkl
//random strings starting from there
iEwcvbnm
dfgwhjkl
iEwhjkl
iEwdä䊀@@Àþ))þÿÿÿJsDw=tDwà@Dwà@

这是我到目前为止编写的代码:

#include <stdio.h>
#include <stdlib.h>
#define N 5
#define DIM 10

int main()
{
    int i=0, c=0, j=0;
    char matrice[DIM][DIM];

    FILE *ifp, *ofp;

    ifp=fopen("C:\\Users\\messa\\OneDrive\\PoliTo\\Anno II\\Algoritmi\\Laboratori\\lab2\\es2\\input.txt", "r");
    if (ifp==NULL) printf("Impossibile aprire file!\n");

    ofp=fopen("C:\\Users\\messa\\OneDrive\\PoliTo\\Anno II\\Algoritmi\\Laboratori\\lab2\\es2\\output.txt", "w");
    if (ofp==NULL) printf("Impossibile aprire file!\n");

    for(i=0; i<DIM-1;i++){
        for(j=0;j<DIM-1;j++){
            fscanf(ifp,"%c", &matrice[i][j]);
        }
    }

    for(i=0; i<DIM; i++) fprintf(ofp, "%s", matrice[i]);

    fclose(ofp);
    fclose(ifp);


    return 0;
}

3 个答案:

答案 0 :(得分:1)

  • 如果您使用'\0',则必须在字符串末尾添加空字符%s
  • matrice[DIM-1]未初始化且其内容不确定。

char matrice[DIM][DIM];更改为char matrice[DIM][DIM]=enter image description here;,以便初始化并在每个字符串后面加'\0',最后一行将为空字符串。

答案 1 :(得分:0)

"%s"打印以空字符结尾的字符串,但您的矩阵不会以空值终止。使用两个嵌套循环进行输出和"%c"格式。或者,使用每行中未使用的最后一个元素放置一个空字符'\0'

答案 2 :(得分:0)

以下代码:

  • 干净地编译
  • 正确读入输入文件,每行matrice一行
  • 在使用过的matrice行结束时停止输出
  • 包含对正在进行的操作的评论
  • 删除不需要的#defineint变量
  • 记录了
  • 所使用的变量
  • 将在文件结尾处正确停止读取输入文件
  • 从输入文件中读取完整行,包括换行符
  • 所以打印行不需要调整&#39;或额外的printf()格式化
#include <stdio.h>
#include <stdlib.h>

// wrap #define values in parens to avoid 'text substitution' errors
// only #define items actually used in the code
// make the matrice plenty big so acquire full input line
#define DIM (50)

int main()
{
    // only declare one variable per statement, for readability, understandability and ease of documentation
    int i; // tracks which row of matrix on input, total rows on output
    int j; // tracks which row of matrix on output
    char matrice[DIM][DIM] = {{'\0'}};  // pre initialize so easy to print as strings

    FILE *ifp = NULL;
    FILE *ofp = NULL;

    ifp=fopen("C:\\Users\\messa\\OneDrive\\PoliTo\\Anno II\\Algoritmi\\Laboratori\\lab2\\es2\\input.txt", "r");
    if (ifp==NULL)
    { // fopen failed, report it, (nothing to cleanup), exit
        perror("fopen for input.txt file failed");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    ofp=fopen("C:\\Users\\messa\\OneDrive\\PoliTo\\Anno II\\Algoritmi\\Laboratori\\lab2\\es2\\output.txt", "w");
    if (ofp==NULL)
    { // then fopen failed, report it, cleanup, exit
        perror("fopen for output.txt file failed");
        fclose( ifp ); // cleanup
        exit( EXIT_FAILURE );
    }

    // implied else fopen successful

    // following loop stops when at end of input file or matrix is full
    // Note: input file is formatted in lines, so read it line by line
    // NOte: fgets() reads in full line from input file, including the 'newline'
    //       so no need to append newline when printing matrix later
    for( i=0; (i < DIM) && fgets( &matrice[i][0], DIM, ifp ); i++);

    // Note: 'i' now contains number of used rows in matrice

    // print contents of matrix
    for(j=0; j<i; j++) fprintf(ofp, "%s", matrice[j]);

    // cleanup
    fclose(ofp);
    fclose(ifp);


    return 0;
} // end function: main