| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 334 人关注过本帖
标题:[求助] C語言關於開啟資料夾 讀取和儲存
只看楼主 加入收藏
cjw23529
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-3-22
收藏
 问题点数:0 回复次数:0 
[求助] C語言關於開啟資料夾 讀取和儲存


我的編程出現錯誤 在case'4' 和 case'5' 這兩個地方
case'4'是儲存檔案在資料夾 case'5'是讀取檔案從資料夾
不知道哪位高手幫幫忙
謝謝

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedlist.h"


int main(int argc, char *argv[])
{

int i;
struct Info info;
char selection;
FILE *fptr;
int size;


printf("Welcome to the storage room.\n\n");


do
{


printf("Please select an option that you wish.");
printf("\n*************************************\n");
printf("Press 1 to add a record.\n");
printf("Press 2 to delete a record.\n");
printf("Press 3 to view all records.\n");
printf("Press 4 to saving records to a disk file.\n");
printf("Press 5 to restoring records from a disk file.\n");
printf("Press 'ESC' to exit.");
printf("\n*************************************\n");
printf("\n");

selection = getch();


switch( selection )
{

case '1':


if (isEmptyList())
{
printf("The list is empty.\n");
}


for (i=0; i<50; i++)
{

printf("record number: ");


scanf(" %d", &info.record);

printf("Name of compenent type: ");
fflush(stdin);


gets(info.type);


printf("The value of component: ");
fflush(stdin);

gets(info.value);


printf("How many in the stock: ");


scanf(" %d", &info.number);

insertNode(&info);
}
break;


case '2':

if (isEmptyList())
{
printf("The list is empty.\n");
}

while(traversalList());

deleteNode();
break;


case '3':


if (isEmptyList())
{
printf("The list is empty.\n");
break;
}


rewindList();


do
{

retrieveNode(&info);


printf("record number\t%d\n", info.record);
printf("compenent type:\t%s\n", info.type);
printf("component value:\t%s\n", info.value);
printf("stock:\t%3d\n", info.number);
} while (traversalList());
break;

case '4'

fptr = fopen("comp.dat","wb");//open the file for writing

while(!isEmptyList())
{
rewindList();
retrieveNode(&info);
fwrite(&info,sizeof(info),1, fptr);
deleteNode();
}


fclose(fptr);


break;

case '5'

fptr = fopen("MyData.dat","rb"); //open the file for

reading

//read from the file
while(1)
{
size = fread(&info,sizeof(info),1,fptr);
if (size <= 0)
{
break;
}


insertNode(&info);

fclose(fptr);

case 27:
printf("Bye.\n\n");
break;

default:
printf("Your selection is not valid.\n");
printf("Please try again\n");
printf("\n");
break;
}

}while (selection != 27);
system("PAUSE");
return 0;
}

以下是linkedlist.c 的部分(應該是沒錯)
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"

//internal function prototypes
void insert();
void create();
void assign(struct Info *info);


struct Node *head=NULL, *current=NULL, *temp=NULL;


int isEmptyList()
{
return (head==NULL);
}


void rewindList()
{
current = head;
}


int traversalList()
{
if (current==NULL) return 0;
if (current->link==NULL) return 0;
current = current->link;
return 1;
}


void insert()
{
if (head == NULL)
{
head = temp;
current = head;
}
else
{
temp->link = current->link;
current->link = temp;
current = current->link;
}
}


void deleteNode()
{
if (head==NULL) return;
if (current==head)
{
head = current->link;
free(current);
current = head;
}
else
{
temp = head;
while(1)
{
if (temp->link==current) break;
temp = temp->link;
}
temp->link = current->link;
free(current);
current = temp->link;
if (current==NULL) current = head;
}
}


void create()
{
temp = (struct Node *)malloc(sizeof(struct Node));
temp->link = NULL;
}


void assign(struct Info *info)
{
if (temp==NULL) return;
memcpy(&temp->info,info,sizeof(struct Info));
}


void retrieveNode(struct Info *info)
{
if (head==NULL || current==NULL) return;
memcpy(info,&current->info,sizeof(struct Info));
}


void insertNode(struct Info *info)
{
create();
assign(info);
insert();
}


以下是linkedlist.h 的部分(應該是沒錯)
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

//declare struct for data
struct Info
{
int number;
int record;
char type[25];
char value[25];
};

struct Node
{
struct Info info;
struct Node *link;
};

//function prototypes
int isEmptyList();
void rewindList();
int traversalList();

void insertNode(struct Info *info);

void deleteNode();

void retrieveNode(struct Info *info);

#endif

2007-05-30 06:26
快速回复:[求助] C語言關於開啟資料夾 讀取和儲存
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029445 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved