我正在做这个大学项目。基本上我要做的就是使用文本文件。函数emptyLineCounter() 应该计算文件中的空行数。当在 v() 中调用时,它会执行预期的操作,稍后,我在 n() 中调用该函数。这是它失败并返回分段错误的地方。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
void checkArrayStatus() {
//check if dynamic arrays (dynamicke polia) are created
}
int checkFileStatus(FILE *file, const char *filename) {
//checks if file is open or close
if (file != NULL) {
return 1;
} else {
printf("Neotvoreny subor %s\n", filename);
return -1;
}
}
int emptyLineCounter(FILE *file) {
//empty lines counter; number of empty lines == number of meassurments done
int emptyLineCount = 1; //maybe rework this, doesn't count last line
char line[MAXSIZE];
while (fgets(line, sizeof(line), file) != NULL) {
int isEmpty = 1; //assume line empty
for (int i = 0; line[i] != '\0'; i++) {
if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n' && line[i] != '\r') {
isEmpty = 0; // Line is not empty
break;
}
}
if (isEmpty) {
emptyLineCount++;
}
}
return emptyLineCount;
}
FILE* v() {
//declare files as empty values
FILE *dataloger = NULL;
char *output[] = {"ID. mer. modulu:", "Pozícia modulu:", "Typ mer. veliciny:", "Hodnota:", "Cas merania:", "Datum merania:", ""};
//if files not opened try to open them
while (checkFileStatus(dataloger, "dataloger.txt") == -1) {
dataloger = fopen("dataloger.txt", "r");
}
if (1) { //change condition to false if n not pressed, use pointers probably
char line[MAXSIZE];
int pos = 0;
while (fgets(line, sizeof(line), dataloger) != NULL) {
if (pos == 7) {
pos = 0;
}
printf("%s ", output[pos]);
printf("%s", line);
pos++;
}
}
int measurement = emptyLineCounter(dataloger);
return dataloger;
}
int n(FILE **file) {
rewind(*file);
if (checkFileStatus(*file, "dataloger.txt") == -1) {
printf("Checking ... Failing\n");
return -1;
}
printf("passed\n");
printf("HERE: %d", emptyLineCounter(*file));//length of array based on number of measurmenets
/*
int dim1 = 3;
int dim2 = 6; // Number of "inner" arrays
char ***dynamicArray = (char ***)malloc(dim1 * sizeof(char **));
//rework so that it accepts each line instead of input
for (int i = 0; i < dim1; i++) {
dynamicArray[i] = (char **)malloc(dim2 * sizeof(char *));
for (int j = 0; j < dim2; j++) {
dynamicArray[i][j] = (char *)malloc(256 * sizeof(char)); // Assuming a maximum string length of 255 characters
printf("Enter a string for element [%d][%d]: ", i, j);
scanf("%s", dynamicArray[i][j]);
}
}*/
return 0;
}
void c() {
printf("c\n");
}
void s() {
printf("s\n");
}
void h() {
printf("h\n");
}
void z() {
printf("z\n");
}
int main() {
//char to store user option
char opt;
FILE *v_val = NULL;
//while true keep running -> end at option k
while(1) {
scanf("%s", &opt);
switch(opt){
//two cases to include capital/lowercase letters
case 'v':
case 'V':
v_val = v();
//printf("%p", v_val);
break;
case 'n':
case 'N':
n(&v_val);
break;
case 'c':
case 'C':
c();
break;
case 's':
case 'S':
s();
break;
case 'h':
case 'H':
h();
break;
case 'z':
case 'Z':
z();
break;
case 'k':
case 'K':
printf("end\n");
//fclose(v_val);
return 0;
default:
printf("Nesprávna možnosť\n"); //check for what the output should be
break;
printf("\n");
}
}
return 0;
}
我不明白为什么该功能突然不起作用。我怀疑它连接到了我在 v <-> main <-> n 之间传递它们时使用的指针,但没有发现任何错误。
我尝试了你的代码。正如好的评论中所指出的,问题似乎很简单,就像尝试在“scanf”函数中读取一个字符,但表示一个字符数组(字符串)。
scanf("%s", &opt);
在测试您的代码时(仅供参考,我将字面措辞重构为英语)我能够复制分段错误。
craig@Vera:~/C_Programs/Console/Empty/bin/Release$ ./Empty
Enter option: v
Unopened file dataloger.txt
ID. Measurement module: Module #1
Module position: 1
Type of measure quantities: Kilograms
Value: 80
Time of measurement: 10:00:00
Date of measurement: 25-Oct-2023
The empty line count when opening the file is: 1
Enter option: n
Segmentation fault (core dumped)
将 scanf 函数更改为以下语句似乎可以解决该问题。
scanf(" %c", &opt);
注意“%c”之前的空格。
这是代码的重新测试。
craig@Vera:~/C_Programs/Console/Empty/bin/Release$ ./Empty
Enter option: v
Unopened file dataloger.txt
ID. Measurement module: Module #1
Module position: 1
Type of measure quantities: Kilograms
Value: 80
Time of measurement: 10:00:00
Date of measurement: 25-Oct-2023
The empty line count when opening the file is: 1
Enter option: n
passed
HERE: 1
Enter option: n
passed
HERE: 1
Enter option: k
end
因此,在使用“scanf”函数时,请始终了解用作接收器变量的数据类型。