打印从csv文件读取输入的结构链接列表时出现问题
我正在读取一个Csv文件,每行有3个数据。列表本身似乎充满了从Csv文件中获取的数据,因为如果我在Insert_列表中打印F,它会打印正确的内容,但如果我打印列表,它会返回垃圾 我认为问题出在插入列表中,因为这是把列表搞砸的唯一方法,但我找不到哪个是插入列表。 我在列表和打印中尝试了许多版本的输入。我实际使用的似乎是最好的选择,因为至少如果我现在要求在insert_列表中打印Lptr,它会给我正确的输入值。如果我打印列表,它会给我垃圾 我在程序中需要的结构打印从csv文件读取输入的结构链接列表时出现问题,c,linked-list,C,Linked List,我正在读取一个Csv文件,每行有3个数据。列表本身似乎充满了从Csv文件中获取的数据,因为如果我在Insert_列表中打印F,它会打印正确的内容,但如果我打印列表,它会返回垃圾 我认为问题出在插入列表中,因为这是把列表搞砸的唯一方法,但我找不到哪个是插入列表。 我在列表和打印中尝试了许多版本的输入。我实际使用的似乎是最好的选择,因为至少如果我现在要求在insert_列表中打印Lptr,它会给我正确的输入值。如果我打印列表,它会给我垃圾 我在程序中需要的结构 typedef struct
typedef struct product {
int prod_code;
char prod_name[20];
int price;
int stockage;
}product;
typedef struct prod_list {
product product;
struct prod_list *next_ptr;
}prod_list;
typedef prod_list *Prod_ptr;
int threshold=10000;
职能:
void insert_list ( Prod_ptr *lptr , int code, char *name, int price);
void print_list( Prod_ptr *lptr);
主要内容:
大多数问题都源于使用
typedef
作为指向prod\u列表的指针。我删除了它,直接使用了prod\u list*
。这使得代码编写和理解变得更加容易。请参阅我在代码中插入的注释
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct product {
int prod_code;
char prod_name[20];
int price;
int stockage;
} product;
typedef struct prod_list {
product product;
struct prod_list *next_ptr;
} prod_list;
//typedef prod_list *Prod_ptr;
//void insert_list(Prod_ptr *lptr, int code, char *name, int price);
//void print_list(Prod_ptr *lptr);
void insert_list(prod_list **lptr, int code, char *name, int price);
void print_list(prod_list *lptr);
int threshold = 10000;
int main() {
prod_list *lptr = NULL;
FILE *file_ptr;
file_ptr = fopen("C:\\Users\\rbaron\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Debug\\semplice.csv", "r");
if (file_ptr == NULL) {
printf("error program name");
return 1;
}
int code, price;
char name[20];
while (fscanf(file_ptr, "%d,%19[^,],%d", &code, name, &price) == 3) {
//insert_list(&lptr, code, name, price);
insert_list(&lptr, code, name, price);
}
//print_list(&lptr);
print_list(lptr);
fclose(file_ptr);
return 0;
}
//void insert_list(Prod_ptr *lptr, int code, char *name, int price) {
void insert_list(prod_list **lptr, int code, char *name, int price) {
if (*lptr == NULL) {
//Prod_ptr newPtr = malloc(sizeof(prod_list));
prod_list * newPtr = malloc(sizeof(prod_list));
if (newPtr != NULL) {
newPtr->product.prod_code = code;
strcpy(newPtr->product.prod_name, name);
newPtr->product.price = price;
newPtr->product.stockage = rand() % (100001);
newPtr->next_ptr = NULL;
*lptr = newPtr;
}
else {
puts(" memoria esaurita");
}
}
else if ((*lptr)->product.prod_code >= code) {
//Prod_ptr newPtr = malloc(sizeof(prod_list));
prod_list * newPtr = malloc(sizeof(prod_list));
if (newPtr != NULL) {
newPtr->product.prod_code = code;
strcpy(newPtr->product.prod_name, name);
newPtr->product.price = price;
newPtr->product.stockage = rand() % (100001);
newPtr->next_ptr = *lptr;
*lptr = newPtr;
}
else {
puts("mem esaurita");
}
}
else {
insert_list(&((*lptr)->next_ptr), code, name, price);
}
}
//void print_list(Prod_ptr *lptr) {
void print_list(prod_list *lptr) {
prod_list * temp = lptr;
// Not sure what this is doing???
//printf("Input Threshold:");
//while ((scanf("%d", &threshold)) != 1 && threshold > 0) {
// printf("error input");
// scanf("%*[^\n]%*c");
//}
if (temp == NULL) {
puts("Error");
}
else {
for (temp = lptr; temp != NULL; temp = temp->next_ptr) {
if (temp->product.stockage < threshold) {
//printf("Product code: %d\nProduct Name: %s\Product price: %d\Stockage: %d\n\n", temp->product.prod_code, temp->product.prod_name, temp->product.price, temp->product.stockage);
printf("Product code: %d\nProduct Name: %s\nProduct price: %d\nStockage: %d\n\n", temp->product.prod_code, temp->product.prod_name, temp->product.price, temp->product.stockage);
}
}
}
}
#包括“stdio.h”
#包括“stdlib.h”
#包括“string.h”
typedef结构产品{
int prod_代码;
char prod_名称[20];
国际价格;
国际储备;
}产品;
类型定义结构产品列表{
产品;
结构产品列表*下一个ptr;
}产品清单;
//类型定义产品列表*产品ptr;
//无效插入列表(产品ptr*lptr,内部代码,字符*名称,内部价格);
//无效打印列表(产品ptr*lptr);
作废插入清单(产品清单**lptr,内部代码,字符*名称,内部价格);
作废打印列表(产品列表*lptr);
int阈值=10000;
int main(){
产品列表*lptr=NULL;
文件*文件ptr;
文件\u ptr=fopen(“C:\\Users\\rbaron\\Documents\\visualstudio 2015\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Debug\\semplice.csv”,“r”);
if(file_ptr==NULL){
printf(“错误程序名”);
返回1;
}
国际编码,价格;
字符名[20];
而(fscanf(文件“%d、%19[^、]、%d”、&代码、名称和价格)==3){
//插入清单(&lptr、代码、名称、价格);
插入清单(&lptr、代码、名称、价格);
}
//打印列表(&lptr);
打印列表(lptr);
fclose(文件检索);
返回0;
}
//无效插入列表(产品ptr*lptr,内部代码,字符*名称,内部价格){
无效插入列表(产品列表**lptr,整数代码,字符*名称,整数价格){
如果(*lptr==NULL){
//Prod_ptr newPtr=malloc(sizeof(Prod_list));
产品列表*newPtr=malloc(sizeof(产品列表));
如果(newPtr!=NULL){
newPtr->product.prod_code=code;
strcpy(newPtr->product.prod\u name,name);
newPtr->product.price=价格;
newPtr->product.stockage=rand()%(100001);
newPtr->next_ptr=NULL;
*lptr=newPtr;
}
否则{
认沽权(“esaurita备忘录”);
}
}
如果((*lptr)->product.prod_code>=代码),则为else{
//Prod_ptr newPtr=malloc(sizeof(Prod_list));
产品列表*newPtr=malloc(sizeof(产品列表));
如果(newPtr!=NULL){
newPtr->product.prod_code=code;
strcpy(newPtr->product.prod\u name,name);
newPtr->product.price=价格;
newPtr->product.stockage=rand()%(100001);
newPtr->next_ptr=*lptr;
*lptr=newPtr;
}
否则{
看跌期权(“mem esaurita”);
}
}
否则{
插入清单(&(*lptr)->下一个清单)、代码、名称、价格;
}
}
//无效打印列表(产品ptr*lptr){
无效打印列表(产品列表*lptr){
产品列表*temp=lptr;
//不知道这是在做什么???
//printf(“输入阈值:”);
//而((scanf(“%d”(&threshold))!=1&&threshold>0){
//printf(“错误输入”);
//scanf(“%*[^\n]%*c”);
//}
if(temp==NULL){
看跌期权(“错误”);
}
否则{
对于(temp=lptr;temp!=NULL;temp=temp->next_ptr){
如果(温度->产品库存<阈值){
//printf(“产品代码:%d\n产品名称:%s\Product price:%d\Stockage:%d\n\n”,temp->Product.prod\u代码,temp->Product.prod\u名称,temp->Product.price,temp->Product.Stockage);
printf(“产品代码:%d\n产品名称:%s\n产品价格:%d\n库存:%d\n\n”,temp->Product.prod\u代码,temp->Product.prod\u名称,temp->Product.price,temp->Product.stockage);
}
}
}
}
您的大多数问题都源于使用typedef
作为指向prod\u list
的指针。我删除了它,直接使用了prod\u list*
。这使事情更容易编码和理解。请参阅我在代码中插入的注释
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct product {
int prod_code;
char prod_name[20];
int price;
int stockage;
} product;
typedef struct prod_list {
product product;
struct prod_list *next_ptr;
} prod_list;
//typedef prod_list *Prod_ptr;
//void insert_list(Prod_ptr *lptr, int code, char *name, int price);
//void print_list(Prod_ptr *lptr);
void insert_list(prod_list **lptr, int code, char *name, int price);
void print_list(prod_list *lptr);
int threshold = 10000;
int main() {
prod_list *lptr = NULL;
FILE *file_ptr;
file_ptr = fopen("C:\\Users\\rbaron\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Debug\\semplice.csv", "r");
if (file_ptr == NULL) {
printf("error program name");
return 1;
}
int code, price;
char name[20];
while (fscanf(file_ptr, "%d,%19[^,],%d", &code, name, &price) == 3) {
//insert_list(&lptr, code, name, price);
insert_list(&lptr, code, name, price);
}
//print_list(&lptr);
print_list(lptr);
fclose(file_ptr);
return 0;
}
//void insert_list(Prod_ptr *lptr, int code, char *name, int price) {
void insert_list(prod_list **lptr, int code, char *name, int price) {
if (*lptr == NULL) {
//Prod_ptr newPtr = malloc(sizeof(prod_list));
prod_list * newPtr = malloc(sizeof(prod_list));
if (newPtr != NULL) {
newPtr->product.prod_code = code;
strcpy(newPtr->product.prod_name, name);
newPtr->product.price = price;
newPtr->product.stockage = rand() % (100001);
newPtr->next_ptr = NULL;
*lptr = newPtr;
}
else {
puts(" memoria esaurita");
}
}
else if ((*lptr)->product.prod_code >= code) {
//Prod_ptr newPtr = malloc(sizeof(prod_list));
prod_list * newPtr = malloc(sizeof(prod_list));
if (newPtr != NULL) {
newPtr->product.prod_code = code;
strcpy(newPtr->product.prod_name, name);
newPtr->product.price = price;
newPtr->product.stockage = rand() % (100001);
newPtr->next_ptr = *lptr;
*lptr = newPtr;
}
else {
puts("mem esaurita");
}
}
else {
insert_list(&((*lptr)->next_ptr), code, name, price);
}
}
//void print_list(Prod_ptr *lptr) {
void print_list(prod_list *lptr) {
prod_list * temp = lptr;
// Not sure what this is doing???
//printf("Input Threshold:");
//while ((scanf("%d", &threshold)) != 1 && threshold > 0) {
// printf("error input");
// scanf("%*[^\n]%*c");
//}
if (temp == NULL) {
puts("Error");
}
else {
for (temp = lptr; temp != NULL; temp = temp->next_ptr) {
if (temp->product.stockage < threshold) {
//printf("Product code: %d\nProduct Name: %s\Product price: %d\Stockage: %d\n\n", temp->product.prod_code, temp->product.prod_name, temp->product.price, temp->product.stockage);
printf("Product code: %d\nProduct Name: %s\nProduct price: %d\nStockage: %d\n\n", temp->product.prod_code, temp->product.prod_name, temp->product.price, temp->product.stockage);
}
}
}
}
#包括“stdio.h”
#包括“stdlib.h”
#包括“string.h”
typedef结构产品{
int prod_代码;
char prod_名称[20];
国际价格;
国际储备;
}产品;
类型定义结构产品列表{
产品;
结构产品列表*下一个ptr;
}产品清单;
//类型定义产品列表*产品ptr;
//无效插入列表(产品ptr*lptr,内部代码,字符*名称,内部价格);
//无效打印列表(产品ptr*lptr);
作废插入清单(产品清单**lptr,内部代码,字符*名称,内部价格);
作废打印列表(产品列表*lptr);
int阈值=10000;
int main(){
产品列表*lptr=NULL;
文件*文件ptr;
文件\u ptr=fopen(“C:\\Users\\rbaron\\Documents\\visualstudio 2015\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Debug\\semplice.csv”,“r”);
if(file_ptr==NULL){
printf(“错误程序名”);
返回1;
}
国际编码,价格;
字符名[20];
而(fscanf(文件“%d、%19[^、]、%d”、&代码、名称和价格)==3){
//插入清单(&lptr、代码、名称、价格);
插入清单(&lptr、代码、名称、价格);
}
//打印列表(&lptr);
打印列表(lptr);
fclose(文件检索);
返回0;
}
//无效插入列表(产品ptr*lptr,内部代码,字符*名称,内部价格){
无效插入列表(产品列表**lptr,整数代码,字符*名称,整数价格){
如果(*lptr==NULL){
//Prod_ptr newPtr=malloc(sizeof(Prod_list));
产品清单*
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct product {
int prod_code;
char prod_name[20];
int price;
int stockage;
} product;
typedef struct prod_list {
product product;
struct prod_list *next_ptr;
} prod_list;
//typedef prod_list *Prod_ptr;
//void insert_list(Prod_ptr *lptr, int code, char *name, int price);
//void print_list(Prod_ptr *lptr);
void insert_list(prod_list **lptr, int code, char *name, int price);
void print_list(prod_list *lptr);
int threshold = 10000;
int main() {
prod_list *lptr = NULL;
FILE *file_ptr;
file_ptr = fopen("C:\\Users\\rbaron\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Debug\\semplice.csv", "r");
if (file_ptr == NULL) {
printf("error program name");
return 1;
}
int code, price;
char name[20];
while (fscanf(file_ptr, "%d,%19[^,],%d", &code, name, &price) == 3) {
//insert_list(&lptr, code, name, price);
insert_list(&lptr, code, name, price);
}
//print_list(&lptr);
print_list(lptr);
fclose(file_ptr);
return 0;
}
//void insert_list(Prod_ptr *lptr, int code, char *name, int price) {
void insert_list(prod_list **lptr, int code, char *name, int price) {
if (*lptr == NULL) {
//Prod_ptr newPtr = malloc(sizeof(prod_list));
prod_list * newPtr = malloc(sizeof(prod_list));
if (newPtr != NULL) {
newPtr->product.prod_code = code;
strcpy(newPtr->product.prod_name, name);
newPtr->product.price = price;
newPtr->product.stockage = rand() % (100001);
newPtr->next_ptr = NULL;
*lptr = newPtr;
}
else {
puts(" memoria esaurita");
}
}
else if ((*lptr)->product.prod_code >= code) {
//Prod_ptr newPtr = malloc(sizeof(prod_list));
prod_list * newPtr = malloc(sizeof(prod_list));
if (newPtr != NULL) {
newPtr->product.prod_code = code;
strcpy(newPtr->product.prod_name, name);
newPtr->product.price = price;
newPtr->product.stockage = rand() % (100001);
newPtr->next_ptr = *lptr;
*lptr = newPtr;
}
else {
puts("mem esaurita");
}
}
else {
insert_list(&((*lptr)->next_ptr), code, name, price);
}
}
//void print_list(Prod_ptr *lptr) {
void print_list(prod_list *lptr) {
prod_list * temp = lptr;
// Not sure what this is doing???
//printf("Input Threshold:");
//while ((scanf("%d", &threshold)) != 1 && threshold > 0) {
// printf("error input");
// scanf("%*[^\n]%*c");
//}
if (temp == NULL) {
puts("Error");
}
else {
for (temp = lptr; temp != NULL; temp = temp->next_ptr) {
if (temp->product.stockage < threshold) {
//printf("Product code: %d\nProduct Name: %s\Product price: %d\Stockage: %d\n\n", temp->product.prod_code, temp->product.prod_name, temp->product.price, temp->product.stockage);
printf("Product code: %d\nProduct Name: %s\nProduct price: %d\nStockage: %d\n\n", temp->product.prod_code, temp->product.prod_name, temp->product.price, temp->product.stockage);
}
}
}
}
void insert_list(struct prodlist **pp, int code , char *name , int price)
{
struct prodlist *newp ;
// Advance pp, until it points to the pointer
// That *should* be pointing at the new entry
// That pointer *could* be NULL. In that case, we reached the end of the list;
for( ; *pp; pp = &(*pp)->next){
if((*pp)->product.prod_code >= code) break; // Found the place!
}
newp = malloc(sizeof *newp);
if(!newp ){
fprintf(stderr, "memoria esaurita\n");
return;
}
newp->product.prod_code = code;
strcpy(newp->product.prod_name , name);
newp->product.price = price;
newp->product.stockage = rand() % 100001;
newp->next = *pp;
*pp = newp;
}