在文件中读取c小错误大问题

时间:2015-11-09 01:24:11

标签: c file matrix fopen

我正在尝试从2个数据文本文件填充2个矩阵,但是编译器告诉我“infiles”(1& 2)的类型有冲突但是如果我拿出FILE我会得到一个转换错误,如果我采取out 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);
}

2 个答案:

答案 0 :(得分:2)

您的错误来自于错误地从文件打开功能声明返回值。

请使用:

,而不是使用问题代码
FILE *infile1 = fopen("m1.dat","r");
FILE *infile2 = fopen("m2.dat","r");

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

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

答案 1 :(得分:2)

如果您检查已编辑代码的退出状态,您会发现在第一次检查文件后退出是1。在您的代码中,您有:

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

每次都会运行exit(1)函数(即使infile1正常)。您需要在exit的支票范围内正确包含{...}。 e.g:

if (infile1 == NULL) {  //testing files
    perror ("File 1 Missing");
    exit (1);
}

infile2相同。我没有检查你的其余代码。