我需要获得此输出:
P1 0 3
P2 1 6
P3 4 4
P4 6 2
但相反,我得到了这个:
0 0 0
0 0 0
0 0 0
0 0 0
我是C编程新手。所以,我不知道为什么我输错了。这就是我做的。我应该将每个列存储在不同的变量中。因为在将它们存储在不同的变量后,我需要为它们进行数学计算。
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
int main (int argc, char *argv[]) {
FILE *fptr;
fptr = fopen(argv[1], "r");
if (fptr != NULL) {
size_t i;
int SIZE = 1000;
int*x[500];
int*y[500];
int*z[500];
int ch=0;
int l;
int n=0;
char line[100];
while ((ch=fgetc (fptr))!= EOF) {
if (ch=='\n')
n++;
}
printf("Number of lines = %d\n", n);
for (l=0; l<n;++l) {
while(fgets(line,sizeof line,fptr)) {
printf("%s",line);
sscanf(line,"%d %d %d",&x[l],&y[l],&z[l]);
}
}
for (i=0; i<l;++i) {
printf("%d %d %d\n",x[i],y[i],z[i]);
}
fclose(fptr);
return 0 ;
}
答案 0 :(得分:1)
我至少看到了两个问题。
fptr
在计算行数时已经到了文件末尾。因此,rewind()
fptr
计算行数后,因为它已经上升到文件末尾。所以在第一次循环后添加rewind(fptr)
。
您不需要x, y, z
作为指针数组,而只需要一个数组。所以将它们改为
int x[500];
int y[500];
int z[500];
答案 1 :(得分:0)
好的,我们先用评论对此进行排序:
#include<stdio.h>
// i dont think the following includes are nessesary for the given code
//#include<stdlib.h>
//#include <string.h>
//#include <math.h>
//#include <stdint.h>
//#include <stdbool.h>
int main (int argc, char *argv[]) {
FILE *fptr;
// i wonder a little about what youre opening here with argv[1]
// since arrays start counting at 0
// for testing and more understandability maybe just put the file
// where your code is and deliver its filename as a string?
fptr = fopen(argv[1], "r");
if (fptr != NULL) {
// whats that? you use it as a counting variable, make it int
// not size_t
size_t i;
// its not good practive to have a variable in capitals
// if you wanna make it a constant, you should define it
// at the beginning of the code, right after your includes
int SIZE = 1000;
// dont use pointers (these -> "*") here! Maybe you mix up
// dynamic array declaration (which requires using malloc() and
// no brackets like "int * dynArray;") with static arrays
// correct usage would be: int x[500]; thats it
int*x[500];
int*y[500];
int*z[500];
int ch=0;
// just a hint for more readability: use "j" instead of "l" but
// thats really up to you :D
int l;
int n=0;
char line[100];
// here you loop through your file char by char and just count
// the lines. Why dont pick the chars on the fly and actually
// write something into your defined arrays x, y, z ?
// some additional conditions (if) may do the trick
while ((ch=fgetc (fptr))!= EOF) {
if (ch=='\n')
n++;
}
printf("Number of lines = %d\n", n);
// sizeof is a function, so youre missing the parentheses
// and above that, you already know the size of your line array
// because its statically defined to hold 100 * sizeof(char)
// my suggestion: trash this entire loop and rethink the previous
for (l=0; l<n;++l) {
while(fgets(line,sizeof line,fptr)) {
printf("%s",line);
sscanf(line,"%d %d %d",&x[l],&y[l],&z[l]);
}
}
// cool loop. but there was no input to x, y, z except the first
// element of your line array, which seems to hold just a 0
// so everythings correct here, the mistake occured before
for (i=0; i<l;++i) {
printf("%d %d %d\n",x[i],y[i],z[i]);
}
//nice, thats important :)
fclose(fptr);
return 0 ;
}
我希望它可以帮助你理清事物并指导一个更清晰的问题,这个问题可能会自己回答? :d