gpt4 book ai didi

c++ - 读取 .txt 文件中的特定单词

转载 作者:太空宇宙 更新时间:2023-11-04 15:44:02 25 4
gpt4 key购买 nike

我有一个包含学生姓名和学号的 txt 文件。我想从他的文件中读取并显示特定的卷号。它只显示第一个卷号,但我想读取第二个人的卷号。

也就是如果我要读取“ss”的卷号,它显示的是第一个人的卷号

程序是

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>

void student_read()
{
clrscr();
char name[30], n[30], temp[30];
int i, roll_no, code, count=0;

ifstream fin("tt.txt",ios::in|ios::beg);
if(!fin)
{
cout << "cannot open for read ";
return;
}
cout << "Enter the name of student" << "\n";
cin >> n;

while(fin >> name >> roll_no)
{
cout << roll_no << endl;
}



if(string[name] == string[n])
{
cout << "roll no" << "\n" << roll_no;
}
else
cout << "Not found";
}

void main()
{
clrscr();
cout << "Students details is" << "\n";
student_read();
getch();
}

txt 文件包含以下数据:

苏拉夫123SS33

最佳答案

您的文本文件中是否有每一行的结尾?你有 sourav 123 ss 33 还是 sourav 123\nss 33?还有这个 if(n[30]==name[30])只比较字符串中的 1 个字符。

关于c++ - 读取 .txt 文件中的特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19510103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com