c - 读入文件c小错误大问题

标签 c file matrix fopen

我试图从 2 个数据文本文件中填充 2 个矩阵,但编译器告诉我“infiles”(1 和 2)的类型存在冲突,但是如果我取出 FILE,我会得到一个转换错误,如果我取出 infile =fopen 我完全没有得到矩阵的输出,但它确实可以编译。似乎没有什么是正确的

问题代码:

FILE *infile1;
FILE *infile2;

*infile1 = (int)fopen("m1.dat","r");
*infile2 = (int)fopen("m2.dat","r");

完整代码:

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <pthread.h>


#define M 4
#define K 4
#define N 4

void *runner(void *param); /* the thread */

int A [M][K];
int B [K][N];
int C [M][N];

struct v {
   int i; // row
   int j; // column
};


int main(int argc, char *argv[]) {

    //using namespace std;
//ifstream infile1 ("m1.dat");
//ifstream infile2 ("m2.dat");

int infile1 = (int)fopen("m1.dat","r");
int infile2 = (int)fopen("m2.dat","r");

FILE *infile1;
FILE *infile2;

//if (!infile1)  //testing files
    // cerr << "Error: could not open input file 1\n";
//if (!infile2)  //testing files
     //cerr << "Error: could not open input file 2\n";

if (!infile1)  //testing files
    fprintf(stderr, "file 1 missing\n");
        exit(1);

if (!infile2)  //testing files
    fprintf(stderr, "file 2 missing\n");
        exit(1);

int i, j, size1, size2 =0;
//infile1 >> size1;
//infile2 >> size2;
fscanf(infile1,"%d",&size1);
fscanf(infile2,"%d",&size2);
float s = (float)size1;
int dim = (int)sqrt(s);


for(i=0;i<M;i++){
 for(j=0;j<N;j++){
  //infile1 >> A[i][j];
 // infile2 >> B[i][j];
  fscanf(infile1,"%d",&A[i][j]);
  fscanf(infile2,"%d",&B[i][j]);
 }
}
 for(i=0;i<M;i++){
    for(j=0;j<N;j++){
    if ((j  % dim)==0) printf("\n");
        printf("%d \t",A[i][j]);
 }
}

printf("\n\n");

  for(i=0;i<M;i++){
    for(j=0;j<N;j++){
    if ((j  % dim)==0) printf("\n");;
        printf("%d \t",B[i][j]);
 }
}

printf("\n\n");

int count = 0;
   for(i = 0; i < M; i++) { //column
      for(j = 0; j < N; j++) { //row
         //Assign a row and column for each thread
         struct v *data = (struct v *) malloc(sizeof(struct v));
         data->i = i;
         data->j = j;
         // Now create the thread passing it data as a parameter
         pthread_t tid;       //Thread ID
         pthread_attr_t attr; //Set of thread attributes
         //Get the default attributes
         pthread_attr_init(&attr);
         //Create the thread
         pthread_create(&tid,&attr,runner,data);
         //Make sure the parent waits for all thread to complete
         pthread_join(tid, NULL);
         count++;
      }
   }

   //Print out the resulting matrix
   for(i = 0; i < M; i++) {
      for(j = 0; j < N; j++) {
         printf("%d ", C[i][j]);
      }
      printf("\n");
   }

   printf("\n \n");

  fclose(infile1);
  fclose(infile2);
return 0;
}

//The thread will begin control in this function
void *runner(void *param) {
   struct v *data = (struct v*)param; // the structure that holds our data
   int n, sum = 0; //the counter and sum

   //Row multiplied by column
   for(n = 0; n< K; n++){
      sum += A[data->i][n] * B[n][data->j];
   }
   //assign the sum to its coordinate
   C[data->i][data->j] = sum;

   pthread_exit(0);
}

编辑:最新版本的代码。仍然只是不向屏幕输出任何内容。甚至有一个 printf 应该在它关闭之前运行,但它甚至不起作用。我从工作版本(它曾经正确地做到这一点)所做的唯一改变是与文件打开相关的事情是在 C++ 中,我将它更改为 C。不知道为什么我没有得到任何错误.这是我最新的完整代码。输出看起来像这样 http://tinypic.com/r/21185u9/9

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <pthread.h>


#define M 4
#define K 4
#define N 4

void *runner(void *param); /* the thread */

int A [M][K];
int B [K][N];
int C [M][N];

struct v {
   int i; // row
   int j; // column
};


int main(int argc, char *argv[]) {

FILE *infile1 = fopen("m1.dat","r");
FILE *infile2 = fopen("m2.dat","r");

if (infile1 == NULL)  //testing files
//if (!infile1)  //testing files
    //fprintf(stderr, "file 1 missing\n");
    perror("File 1 Missing");
        exit(1);

if (infile2 == NULL)  //testing files
//if (!infile1)  //testing files
    //fprintf(stderr, "file 1 missing\n");
    perror("File 2 Missing");
        exit(1);

int i, j, size1, size2 =0;

fscanf(infile1,"%d",&size1);
fscanf(infile2,"%d",&size2);
float s = (float)size1;
int dim = (int)sqrt(s);


for(i=0;i<M;i++){
 for(j=0;j<N;j++){
  fscanf(infile1,"%d",&A[i][j]);
  fscanf(infile2,"%d",&B[i][j]);
 }
}
 for(i=0;i<M;i++){
    for(j=0;j<N;j++){
    if ((j  % dim)==0) printf("\n");
        printf("%d \t",A[i][j]);
 }
}

printf("\n\n");

  for(i=0;i<M;i++){
    for(j=0;j<N;j++){
    if ((j  % dim)==0) printf("\n");;
        printf("%d \t",B[i][j]);
 }
}

printf("\n\n");

int count = 0;
   for(i = 0; i < M; i++) { //column
      for(j = 0; j < N; j++) { //row
         //Assign a row and column for each thread
         struct v *data = (struct v *) malloc(sizeof(struct v));
         data->i = i;
         data->j = j;
         // Now create the thread passing it data as a parameter
         pthread_t tid;       //Thread ID
         pthread_attr_t attr; //Set of thread attributes
         //Get the default attributes
         pthread_attr_init(&attr);
         //Create the thread
         pthread_create(&tid,&attr,runner,data);
         //Make sure the parent waits for all thread to complete
         pthread_join(tid, NULL);
         count++;
      }
   }

   //Print out the resulting matrix
   for(i = 0; i < M; i++) {
      for(j = 0; j < N; j++) {
         printf("%d ", C[i][j]);
      }
      printf("\n");
   }

   printf("\n \n");
   printf("program ran");

    fclose(infile1);
    fclose(infile2);
return 0;

}


//The thread will begin control in this function
void *runner(void *param) {
   struct v *data = (struct v*)param; // the structure that holds our data
   int n, sum = 0; //the counter and sum

   //Row multiplied by column
   for(n = 0; n< K; n++){
      sum += A[data->i][n] * B[n][data->j];
   }
   //assign the sum to its coordinate
   C[data->i][data->j] = sum;
   pthread_exit(0);
}

最佳答案

您的错误来自于错误地声明了文件打开函数的返回值。

不要使用问题代码,而是使用:

FILE *infile1 = fopen("m1.dat","r");
FILE *infile2 = fopen("m2.dat","r");

它将 infile1 和 infile2 声明为指向 FILE 类型的指针,并根据文件打开的成功程度正确设置值。简单来说,调用 infile1 和 infile2 作为文件句柄。

如果你想要一个整数值作为返回值,你可以查看 open() 函数而不是 fopen(),但是 fopen() 适用于您的情况。

关于c - 读入文件c小错误大问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33601126/

相关文章:

java - JAVA 中的符号扩展、位移位。帮助理解 C 代码位

c - 是否可以对管道进行读而不是 block ,而是写 block ?

c++ - 数组中的最大 X 值

c++ - 使用 makefile 在最终可执行文件之前创建库

Java 用数据填充预分配文件(在驱动器上)

matlab - 如何使用 `cellfun` (MATLAB) 应用具有多个参数的函数?

java - URL.openStream() 与 respone.getEntity().getContent() 相同吗?

file - Applescript 将文件复制到新文件夹

arrays - 二维数组和一维数组相加的数学解释是什么?

python - 将数组 (k,) 或 (k, n) 乘以一维数组 (k,)