在刚刚开始编写java代码之前,我是新手在c ++programmıng中编写代码。我尝试将teh txt文件解析为数据库。但我采取这个错误我在互联网上搜索我找不到确切的答案?如果你知道请帮助我。感谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void menu() {
puts("1. List the products");
puts("2. Add a new product");
puts("3. Sell the new product");
puts("4. Search by Barcode");
puts("5. Exit");
}
int main(int argc, char *argv[]) {
FILE *fProduct;
char name[20];
int quantity;
int barcode;
double price;
menu();
fProduct = fopen("Product.txt", "a+");
if (fProduct != NULL) {
while (!feof(fProduct))
{
printf("Name :");
scanf("%s" , name);
printf("Quantity :");
scanf("%d", &quantity);
printf("Barcode Number :");
scanf("%d", &barcode);
printf("Price :");
scanf("%lf", &price);
printf("Are there any product ???");
}
}
fclose(fProduct);
}
答案 0 :(得分:2)
fclose
已应用parameter validation assertion。
fclose
功能关闭stream
。如果stream
为NULL
,则调用无效参数处理程序,如参数验证中所述。 ...
在Debug版本中,无效参数宏通常会在调用调度函数之前引发失败的断言和调试器断点。 ...
将您的fclose
来电移至if
阻止NULL
区域内。
答案 1 :(得分:1)
根据您的屏幕截图,您遇到链接器错误,因此您可能无法运行正确的代码版本。基于错误消息,我猜测问题是scanf将数据加载到name参数中。 1)做一个干净的构建,并确保你没有得到任何构建或链接器错误。 2)如果错误仍然发生,则按屏幕上的重试,调试器将显示导致问题的行。使用堆栈窗口在堆栈中查找代码。
答案 2 :(得分:0)
如果你想在C ++中使用它,最好编写C ++代码而不是C代码:
// The headers to include are different
#include <iostream>
#include <fstream>
#include <string>
void menu();
int main() {
using namespace std;
ofstream fProduct;
string name;
int quantity;
int barcode;
double price;
// your menu, but for now we handle only the
// case 2: add a new product.
menu();
// as for now we deal only with the insertion of new
// products
// Let's open a new file using the C++ standard library
fProduct.open("Product.txt", ios::out | ios::app | ios::ate);
// If the write can be opened we continue, otherwise
// we skip the next part of the code
if (fProduct.is_open()) {
string conts = "y";
while (conts == "y") {
cout << "Name: "; cin >> name;
cout << "Quantity: "; cin >> quantity;
cout << "Barcode Number: "; cin >> barcode;
cout << "Price: "; cin >> price;
// Here we write in the file the information the user passed us.
// since we are getting information that should be written as
// sequence of char in the file, we could avoid to use
// int/double variables. Let's write in the file, comma
// separated
fProduct << name << "," << quantity << "," << barcode << "," << price << endl;
// Here we have some way to interrupt the loop
cout << "Add another product? [y/n]";
cin >> conts;
}
// Finally we close the file. (only if it was open...)
fProduct.close();
}
return 0;
}
// Your menu function using the standard streams
void menu() {
using namespace std;
cout << "1. List the products" << endl;
cout << "2. Add a new product" << endl;
cout << "3. Sell the new product" << endl;
cout << "4. Search by Barcode" << endl;
cout << "5. Exit" << endl;
}
如果你想要它,那么使用纯C代码会更好:
#include <stdio.h>
#include <string.h>
void menu();
int main() {
FILE *fProduct;
// again, all this variable could be simply char[].
char name[20];
int quantity, barcode;
double price;
// As for now our code handles only the insertion of
// new element, not the other choices of the menu
menu();
// Do not check if it is different than NULL,
// check only if it actually is something...
if (fProduct = fopen("Product.txt", "a")) {
char conts = 'y';
while (conts == 'y') {
printf("Name :"); scanf("%s", name);
printf("Quantity :"); scanf("%d", &quantity);
printf("Barcode Number :"); scanf("%d", &barcode);
printf("Price :"); scanf("%lf", &price);
// Let's write in the file
fprintf(fProduct, "%s,%d,%d,%lf\n", name, quantity, barcode, price);
getchar(); // chomps the last newline
printf("Add another product? [y/n] ");
conts = getchar();
}
// At the end of our loop we need to close the file.
fclose(fProduct);
}
return 0;
}
void menu() {
puts("1. List the products");
puts("2. Add a new product");
puts("3. Sell the new product");
puts("4. Search by Barcode");
puts("5. Exit");
}