嘿家伙我有这个代码:(我试图读取一个字符串并将其放在输出文件中)
#include "structs.h"
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE* input = fopen("journal.txt", "r");
FILE* output = fopen("output.txt", "w");
char date[9];
if( ferror(input) || ferror(output) ) {
perror("Error opening input/output file\n");
}
fscanf(input, "%s", date);
fgets(date, 9, input);
fputs(date, output);
fclose(input);
fclose(output);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译正确,但在运行时它显示错误
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么:(请帮助
我有一个CSV文件的规范:
我有这个代码来标记每一行:
public WarehouseItem(String warehouseItem) {
String key, brand, model;
int weightInKG;
double price;
StringTokenizer strTok;
strTok = new StringTokenizer(warehouseItem);
try {
key = strTok.nextToken();
brand = strTok.nextToken();
model = strTok.nextToken();
weightInKG = Integer.parseInt(strTok.nextToken());
price = Double.valueOf(strTok.nextToken());
}
catch (Exception e) {
throw new IllegalStateException("CSV row had invalid format");
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到CSV文件的IllegalStateException.
Exception in thread "main" java.lang.IllegalStateException: CSV row had invalid format
at WarehouseItem.<init>(WarehouseItem.java:23) // …Run Code Online (Sandbox Code Playgroud)