从文件中读取的问题 [英] problem with reading from file

查看:45
本文介绍了从文件中读取的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从文件中读取时遇到问题。我想在文件中找到一些

字符串(例如名字),如果存在,则在屏幕上显示

秒和第三行。


我有这个档案:


名字

姓氏

年龄

#include< stdio.h>

#include< conio.h>


文件*文件;

main(){


char s [30];

int line = 0;

char name [] =" kate";

file = fopen(" text.txt"," r");


if(!file) {

printf(文件不存在);

getch();

返回0;

}


while(line< 4)

{


fgets(s,sizeof(s),file );

if(strcmp(s,name)== 0)printf("%s",s);

else {

printf(没找到字符串);

getch();

返回0;

}

line ++;


}

fclose(fi le);

getch();

}

解决方案


mi******@o2.pl 写道:

我从文件中读取有问题。我想在文件中找到一些
字符串(例如名字),如果存在则在屏幕上显示第二和第三行的


我有这个文件:

姓名
姓氏
年龄

#include< stdio.h>
#include< conio.h>

FILE *文件;

main(){




我会在这里指出你需要有int main()你需要

返回一些东西。

你对这段代码有什么问题?




Andrew Poelstra写道:

mi *** ***@o2.pl 写道:

我从文件中读取有问题。我想在文件中找到一些
字符串(例如名字),如果存在则在屏幕上显示第二和第三行的


我有这个文件:

姓名
姓氏
年龄

#include< stdio.h>
#include< conio.h>

FILE *文件;

main(){



我会在这里指出你需要int main( )你需要回复一些东西。
这段代码究竟是什么问题?




对不起;你确实从main返回了一个值。

注意:当我试图编译它时,我发现我没有

conio.h。这个标题应该是什么?




Andrew Poelstra写道:

Andrew Poelstra写道:

mi******@o2.pl 写道:

我从文件中读取有问题。我想在文件中找到一些
字符串(例如名字),如果存在则在屏幕上显示第二和第三行的


我有这个文件:

姓名
姓氏
年龄

#include< stdio.h>
#include< conio.h>

FILE *文件;

main(){



我会在这里指出你需要int main( )你需要回复一些东西。
这段代码到底是什么问题?



抱歉;你确实从main返回了一个值。
注意:当我试图编译它时,我发现我没有
conio.h。这个标题应该做什么?




该标题提供DOS特定的控制台I / O,在Borland中找到

C产品。


出于某种原因,许多新手喜欢使用`getch()`它提供,

而不是标准的`getchar()`。这通常是为了防止程序在IDE中完成后关闭

控制台窗口,这些窗口不会为你保持开放状态。


Hi, I have a problem with reading from file. I would like to find some
string(f e.g. name) in file, and if it exist show it on screen with
second and third line under.

I have this in file:

name
surname
age
#include <stdio.h>
#include <conio.h>

FILE *file;

main(){

char s[30];
int line = 0;
char name[]="kate";
file=fopen("text.txt","r");

if(!file) {
printf("file doesnt exist");
getch();
return 0;
}

while( line<4 )
{

fgets(s, sizeof(s), file);
if (strcmp(s,name)==0) printf("%s",s);
else {
printf("didnt find a string");
getch();
return 0;
}
line++;

}
fclose (file);
getch();
}

解决方案


mi******@o2.pl wrote:

Hi, I have a problem with reading from file. I would like to find some
string(f e.g. name) in file, and if it exist show it on screen with
second and third line under.

I have this in file:

name
surname
age
#include <stdio.h>
#include <conio.h>

FILE *file;

main(){



I''ll point out here that you need to have int main() and you need to
return something.
What exactly is your problem with this code?



Andrew Poelstra wrote:

mi******@o2.pl wrote:

Hi, I have a problem with reading from file. I would like to find some
string(f e.g. name) in file, and if it exist show it on screen with
second and third line under.

I have this in file:

name
surname
age
#include <stdio.h>
#include <conio.h>

FILE *file;

main(){



I''ll point out here that you need to have int main() and you need to
return something.
What exactly is your problem with this code?



Sorry; you did return a value from main.
Note: When I attempted to compile this, I found that I did not have
conio.h. What is this header supposed to do?



Andrew Poelstra wrote:

Andrew Poelstra wrote:

mi******@o2.pl wrote:

Hi, I have a problem with reading from file. I would like to find some
string(f e.g. name) in file, and if it exist show it on screen with
second and third line under.

I have this in file:

name
surname
age
#include <stdio.h>
#include <conio.h>

FILE *file;

main(){



I''ll point out here that you need to have int main() and you need to
return something.
What exactly is your problem with this code?



Sorry; you did return a value from main.
Note: When I attempted to compile this, I found that I did not have
conio.h. What is this header supposed to do?



That header provides DOS-specific console I/O, and is found in Borland
C products.

For some reason, lots of newbies like to use `getch()` it provides,
instead of the standard `getchar()`. This is usually to prevent the
console window closing after the program finishes in IDEs that won''t
keep them open for you.


这篇关于从文件中读取的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆