读入文件c小错误大问题
reading in a file c small error big problems
我正在尝试从 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);
}
您的错误来自文件打开函数中错误地声明 return 值。
不要使用问题代码,而是使用:
FILE *infile1 = fopen("m1.dat","r");
FILE *infile2 = fopen("m2.dat","r");
它将 infile1 和 infile2 声明为指向 FILE 类型的指针,并根据文件打开的成功程度正确设置了值。简单来说,调用infile1和infile2作为文件句柄。
如果你想要一个整数值作为 return 值,你可以查看 open()
函数而不是 fopen()
,但 fopen()
在你的情况下效果很好.
如果您检查已编辑代码的退出状态,您会发现它是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
。例如:
if (infile1 == NULL) { //testing files
perror ("File 1 Missing");
exit (1);
}
infile2
也是如此。我没有检查你的代码的其余部分。
我正在尝试从 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);
}
您的错误来自文件打开函数中错误地声明 return 值。
不要使用问题代码,而是使用:
FILE *infile1 = fopen("m1.dat","r");
FILE *infile2 = fopen("m2.dat","r");
它将 infile1 和 infile2 声明为指向 FILE 类型的指针,并根据文件打开的成功程度正确设置了值。简单来说,调用infile1和infile2作为文件句柄。
如果你想要一个整数值作为 return 值,你可以查看 open()
函数而不是 fopen()
,但 fopen()
在你的情况下效果很好.
如果您检查已编辑代码的退出状态,您会发现它是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
。例如:
if (infile1 == NULL) { //testing files
perror ("File 1 Missing");
exit (1);
}
infile2
也是如此。我没有检查你的代码的其余部分。