C语言文件读取问题,我用win-tc操作的
我不清楚要读的文件放哪,试过很多地方都不对,输出一直是"cannotopenfile".我要把一个文件的第1+3n空格(包括该空格)后的非空格数据输入到另一个空TXT文件...
我不清楚要读的文件放哪,试过很多地方都不对,输出一直是"cannot open file".
我要把一个文件的第1+3n空格(包括该空格)后的非空格数据输入到另一个空TXT文件,也请高手帮我看下代码有没有问题,我刚刚入门文件内容,赐教一二.感谢.
main()
{ FILE *fp,*fg;
int i=0,j=0;
char ch;
if((fp=fopen("LandscapeHeight","rt"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
if((fg=fopen("LandscapeHeights","wt"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==' ')
j++;
if(j%3==1)
{
fputc(ch,fg);
i++;
}
ch=fgetc(fp);
}
fclose(fp);
fclose(fg);
getch();
}
不行,加了扩展名&放在C盘 还是"cannot open file".你用WIN-TC试试.
而且直接追踪地址"C:\Users\Administrator\Desktop\LandscapeHeight.txt"也还是"cannot open file".这个是放在桌面的文件.
在VC6倒是可以顺利运行,是WIN-TC的问题吗?我的是WIN7. 展开
我要把一个文件的第1+3n空格(包括该空格)后的非空格数据输入到另一个空TXT文件,也请高手帮我看下代码有没有问题,我刚刚入门文件内容,赐教一二.感谢.
main()
{ FILE *fp,*fg;
int i=0,j=0;
char ch;
if((fp=fopen("LandscapeHeight","rt"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
if((fg=fopen("LandscapeHeights","wt"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==' ')
j++;
if(j%3==1)
{
fputc(ch,fg);
i++;
}
ch=fgetc(fp);
}
fclose(fp);
fclose(fg);
getch();
}
不行,加了扩展名&放在C盘 还是"cannot open file".你用WIN-TC试试.
而且直接追踪地址"C:\Users\Administrator\Desktop\LandscapeHeight.txt"也还是"cannot open file".这个是放在桌面的文件.
在VC6倒是可以顺利运行,是WIN-TC的问题吗?我的是WIN7. 展开
4个回答
展开全部
我用的是Win-TC编译的,你试试吧。
/*头文件*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "mem.h"
#include "ctype.h"
#include "alloc.h"
typedef struct zl /*定义数据结构*/
{ char name[12];
char addr[12];
char tele[12];
struct zl *next;
}people;
/*函数原形*/
people *init();
people *creat();
people *del(people *h);
void print(people *h);
void search(people *h);
void save(people *h);
people *load();
people *insert(people *h);
int menu_select();
people *load();
/*主函数*/
main()
{
int i;
people *head;
head=init();
clrscr();
for(;;)
{
switch(menu_select())
{
case 1:head=init();break;
case 2:head=creat();break;
case 3:head=insert(head);break;
case 4:search(head);break;
case 5:head=del(head);break;
case 6:print(head);break;
case 8:save(head);break;
case 9:head=load();break;
case 0:exit(0);
}
}
}
/*菜单函数*/
menu_select()
{
char *menu[]={"**************menu**************",
"1.init list",
"2.Enter list",
"3.insert record to list",
"4.search record on name",
"5.delete a record from list",
"6.print list",
"8.save the file",
"9.Load the file",
"0.Quit"};
char s[3];
int c,i;
gotoxy(1,25);
printf("press any key to enter menu......\n");
getch();
clrscr();
gotoxy(1,1);
textcolor(YELLOW);
textbackground(BLUE);
gotoxy(10,2);
putch(0xc9);
for(i=1;i<44;i++)
putch(0xcd);
putch(0xbb);
for(i=3;i<20;i++)
{
gotoxy(10,i);putch(0xba);
gotoxy(54,i);putch(0xba);
}
gotoxy(10,20);putch(0xc8);
for(i=1;i<44;i++)
putch(0xcd);
putch(0xbc);
window(11,3,53,19);
clrscr();
for(i=0;i<16;i++)
{
gotoxy(10,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLACK);
window(1,1,80,25);
gotoxy(10,21);
do{
printf("\n Enter your choice(0-9):");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>9);
return c;
}
/*初始化链表*/
people *init()
{
return NULL;
}
/*创建链表*/
people *creat()
{
int i;
people *h=NULL,*info;
for(i=0;i<3;i++)
{
info=(people *)malloc(sizeof(people));
if(!info)
{
printf("\nout of memory");
return NULL;
}
inputs("enter name:",info->name,12);
inputs("enter addr:",info->addr,12);
inputs("enter tele:",info->tele,12);
info->next=h;
h=info;
}
return h;
}
/*输出链表信息*/
void print(people *h)
{
people *p;
clrscr();
p=h;
printf("\n\n\n****************people****************\n");
printf("| name | addr | tele |\n");
printf("|------------|------------|------------|\n");
while(p!=NULL)
{
printf("|%-12s|%-12s|%-12s|\n",p->name,p->addr,p->tele);
p=p->next;
}
printf("*****************end*******************\n");}
/*删除记录*/
people *del(people *h)
{ people *p,*q;
char s[10];
clrscr();
printf("please enter the name you want to delete\n");
scanf("%s",s);
q=p=h;
while(strcmp(p->name,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
printf("\nlist no %s people\n",s);
else
{
printf("******************have found**************\n");
printf("| name | addr | tele |\n");
printf("|------------|------------|------------|\n");
printf("|%-12s|%-12s|%-12s|\n",p->name,p->addr,p->tele);
printf("******************end*******************\n");
getch();
if(p==h)
h=p->next;
else
q->next=p->next;
free(p);
printf("\n have deleted name %s people\n",s);
printf("Don't forget save\n");
}
return(h);
}
/*查找记录*/
void search(people *h)
{
people *p;
char s[10];
clrscr();
printf("please enter name for search\n");
scanf("%s",s);
p=h;
while(strcmp(p->name,s)&&p!=NULL)
p=p->next;
if(p==NULL)
printf("\n list no %s student\n",s);
else
{
printf("******************have found*************\n");
printf("| name | addr | tele |\n");
printf("|------------|------------|------------|\n");
printf("|%-12s|%-12s|%-12s|\n",p->name,p->addr,p->tele);
printf("*****************end********************\n");
}
}
/*插入记录*/
people *insert(people *h)
{
people *info;
printf("\n please enter new record\n");
info=(people *)malloc(sizeof(people));
if(!info)
{
printf("\n out of memory");
return NULL;
}
inputs("enter name:",info->name,10);
inputs("enter addr:",info->addr,10);
inputs("enter tele:",info->tele,10);
info->next=h;
h=info;
return h;
}
/*保存数据到文件*/
void save(people *h)
{
FILE *fp;
people *p;
char outfile[10];
printf("Enter outfile name,for example c:\\f1\\te.txt:\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
printf("can not open file\n");
exit(1);
}
printf("\nSaving file......\n");
p=h;
while(p!=NULL)
{
fwrite(p,sizeof(people),1,fp);
p=p->next;
}
fclose(fp);
printf("-----------------save success!---------------\n");
}
/*输入字符串*/
inputs(char *prompt,char *s,int count)
{
char p[255];
do{
printf(prompt);
scanf("%s",p);
if(strlen(p)>count)printf("\n too long!\n");
}
while(strlen(p)>count);
strcpy(s,p);
}
/*从文件中读取数据*/
people *load()
{ people *p,*q,*h=NULL;
FILE *fp;
char infile[10];
printf("Enter infile name,for example c:\\f1\\popo.txt:\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)
{ printf("can not open file\n");
exit(1);
}
printf("\n-----Loading file!----\n");
p=(people *)malloc(sizeof(people));
if(!p)
{
printf("out of memory!\n");
return h;
}
h=p;
while(!feof(fp))
{
if(1!=fread(p,sizeof(people),1,fp))
break;
p->next=(people *)malloc(sizeof(people));
if(!p->next)
{
printf("out of memory!\n");
return h;
}
q=p;
p=p->next;
}
q->next=NULL;
fclose(fp);
printf("---You have success read data from file!---\n");
return h;
}
/*头文件*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "mem.h"
#include "ctype.h"
#include "alloc.h"
typedef struct zl /*定义数据结构*/
{ char name[12];
char addr[12];
char tele[12];
struct zl *next;
}people;
/*函数原形*/
people *init();
people *creat();
people *del(people *h);
void print(people *h);
void search(people *h);
void save(people *h);
people *load();
people *insert(people *h);
int menu_select();
people *load();
/*主函数*/
main()
{
int i;
people *head;
head=init();
clrscr();
for(;;)
{
switch(menu_select())
{
case 1:head=init();break;
case 2:head=creat();break;
case 3:head=insert(head);break;
case 4:search(head);break;
case 5:head=del(head);break;
case 6:print(head);break;
case 8:save(head);break;
case 9:head=load();break;
case 0:exit(0);
}
}
}
/*菜单函数*/
menu_select()
{
char *menu[]={"**************menu**************",
"1.init list",
"2.Enter list",
"3.insert record to list",
"4.search record on name",
"5.delete a record from list",
"6.print list",
"8.save the file",
"9.Load the file",
"0.Quit"};
char s[3];
int c,i;
gotoxy(1,25);
printf("press any key to enter menu......\n");
getch();
clrscr();
gotoxy(1,1);
textcolor(YELLOW);
textbackground(BLUE);
gotoxy(10,2);
putch(0xc9);
for(i=1;i<44;i++)
putch(0xcd);
putch(0xbb);
for(i=3;i<20;i++)
{
gotoxy(10,i);putch(0xba);
gotoxy(54,i);putch(0xba);
}
gotoxy(10,20);putch(0xc8);
for(i=1;i<44;i++)
putch(0xcd);
putch(0xbc);
window(11,3,53,19);
clrscr();
for(i=0;i<16;i++)
{
gotoxy(10,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLACK);
window(1,1,80,25);
gotoxy(10,21);
do{
printf("\n Enter your choice(0-9):");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>9);
return c;
}
/*初始化链表*/
people *init()
{
return NULL;
}
/*创建链表*/
people *creat()
{
int i;
people *h=NULL,*info;
for(i=0;i<3;i++)
{
info=(people *)malloc(sizeof(people));
if(!info)
{
printf("\nout of memory");
return NULL;
}
inputs("enter name:",info->name,12);
inputs("enter addr:",info->addr,12);
inputs("enter tele:",info->tele,12);
info->next=h;
h=info;
}
return h;
}
/*输出链表信息*/
void print(people *h)
{
people *p;
clrscr();
p=h;
printf("\n\n\n****************people****************\n");
printf("| name | addr | tele |\n");
printf("|------------|------------|------------|\n");
while(p!=NULL)
{
printf("|%-12s|%-12s|%-12s|\n",p->name,p->addr,p->tele);
p=p->next;
}
printf("*****************end*******************\n");}
/*删除记录*/
people *del(people *h)
{ people *p,*q;
char s[10];
clrscr();
printf("please enter the name you want to delete\n");
scanf("%s",s);
q=p=h;
while(strcmp(p->name,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
printf("\nlist no %s people\n",s);
else
{
printf("******************have found**************\n");
printf("| name | addr | tele |\n");
printf("|------------|------------|------------|\n");
printf("|%-12s|%-12s|%-12s|\n",p->name,p->addr,p->tele);
printf("******************end*******************\n");
getch();
if(p==h)
h=p->next;
else
q->next=p->next;
free(p);
printf("\n have deleted name %s people\n",s);
printf("Don't forget save\n");
}
return(h);
}
/*查找记录*/
void search(people *h)
{
people *p;
char s[10];
clrscr();
printf("please enter name for search\n");
scanf("%s",s);
p=h;
while(strcmp(p->name,s)&&p!=NULL)
p=p->next;
if(p==NULL)
printf("\n list no %s student\n",s);
else
{
printf("******************have found*************\n");
printf("| name | addr | tele |\n");
printf("|------------|------------|------------|\n");
printf("|%-12s|%-12s|%-12s|\n",p->name,p->addr,p->tele);
printf("*****************end********************\n");
}
}
/*插入记录*/
people *insert(people *h)
{
people *info;
printf("\n please enter new record\n");
info=(people *)malloc(sizeof(people));
if(!info)
{
printf("\n out of memory");
return NULL;
}
inputs("enter name:",info->name,10);
inputs("enter addr:",info->addr,10);
inputs("enter tele:",info->tele,10);
info->next=h;
h=info;
return h;
}
/*保存数据到文件*/
void save(people *h)
{
FILE *fp;
people *p;
char outfile[10];
printf("Enter outfile name,for example c:\\f1\\te.txt:\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
printf("can not open file\n");
exit(1);
}
printf("\nSaving file......\n");
p=h;
while(p!=NULL)
{
fwrite(p,sizeof(people),1,fp);
p=p->next;
}
fclose(fp);
printf("-----------------save success!---------------\n");
}
/*输入字符串*/
inputs(char *prompt,char *s,int count)
{
char p[255];
do{
printf(prompt);
scanf("%s",p);
if(strlen(p)>count)printf("\n too long!\n");
}
while(strlen(p)>count);
strcpy(s,p);
}
/*从文件中读取数据*/
people *load()
{ people *p,*q,*h=NULL;
FILE *fp;
char infile[10];
printf("Enter infile name,for example c:\\f1\\popo.txt:\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)
{ printf("can not open file\n");
exit(1);
}
printf("\n-----Loading file!----\n");
p=(people *)malloc(sizeof(people));
if(!p)
{
printf("out of memory!\n");
return h;
}
h=p;
while(!feof(fp))
{
if(1!=fread(p,sizeof(people),1,fp))
break;
p->next=(people *)malloc(sizeof(people));
if(!p->next)
{
printf("out of memory!\n");
return h;
}
q=p;
p=p->next;
}
q->next=NULL;
fclose(fp);
printf("---You have success read data from file!---\n");
return h;
}
展开全部
main()
{ FILE *fp,*fg;
int i=0,j=0;
char ch;
//if((fp=fopen("LandscapeHeight","rt"))==NULL)
//将LandscapeHeight.txt这个文件放在C盘的根目录下。
if((fp=fopen("C:\\LandscapeHeight.txt","rt")==NULL)
{
printf("cannot open file\n");
exit(0);
}
//if((fg=fopen("LandscapeHeights","wt"))==NULL)
//在widow系统还是带上扩展名比较好。
if((fg=fopen("LandscapeHeights.txt","wt"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==' ')
j++;
if(j%3==1)
{
fputc(ch,fg);
i++;
}
ch=fgetc(fp);
}
fclose(fp);
fclose(fg);
getch();
}
{ FILE *fp,*fg;
int i=0,j=0;
char ch;
//if((fp=fopen("LandscapeHeight","rt"))==NULL)
//将LandscapeHeight.txt这个文件放在C盘的根目录下。
if((fp=fopen("C:\\LandscapeHeight.txt","rt")==NULL)
{
printf("cannot open file\n");
exit(0);
}
//if((fg=fopen("LandscapeHeights","wt"))==NULL)
//在widow系统还是带上扩展名比较好。
if((fg=fopen("LandscapeHeights.txt","wt"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==' ')
j++;
if(j%3==1)
{
fputc(ch,fg);
i++;
}
ch=fgetc(fp);
}
fclose(fp);
fclose(fg);
getch();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你读的时候,文件名不对,所以读不到,要包括他的格式比如a.txt而不是a
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
原来你使用win7系统啊。
你需要以管理员权限运行win-tc,否则无法读取C盘上一些内容。
放到d盘上就可以了吧。
你需要以管理员权限运行win-tc,否则无法读取C盘上一些内容。
放到d盘上就可以了吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询