search in file

I want to search a txt file, when I find the word,i looking for; how can i go back in file, to search again ??
i close file, and open it agian, to do this,but it doesn't work.

thanks
It should. Can you post your code?
If you use ifstreams, use seekg
I can, but it have several factors, that it can't be understood if i don't post all of it.
and code is too long.

i want to search it, and then, write some charachtre on it, but couse i use loops , i can't close file with " ios::in " mode , and open it with another mode (ios::out),
how can i swich between these, and get back, at first line for search again
i am begginer in this, so can u explain more

"it's my Qestion : a programm for : 1.get information about students,and save them in a txt file
2. delete information ( input charchtre "#" infront of line that we want to delete)
3.search in file

i don't want to use string for this,and search in string, i want ot search file, and add # in file, not to the string;

is it possible??

thank u so much.

char studentnumbers [4];
char s[];
char students[];
fstream file;
file.open("student" , ios::out);
file.get (students, 10000,-1);
file.close ();
file.open("student" , ios::out);
while (! file.eof ())
{

file.get ( studentnumbers , 4) ;

if ( studentnumbers[0] == s[0] && studentnumbers[1] == s[1] )
{
if (studentnumbers[3] == '#')
{
cout << "The student doesn't exist\n\n";
}

else if (studentnumbers[3] != '#')
{
int t = 3 + line;
students[t] = '#';
file.open("student" , ios::out);
file << students;
break;
}


}
else
{
ifile.open ("student",ios::in);
ifile.get(endline,100,'\n');
line += 25;

}


}
I post it somehow, i hope it can be understood.

thank u.
The simplest way to handle this is simply to load the entire file.
Mess with it in memory.
Then rewrite the file to disk.


How it is that you store the file in memory depends on the file's content and how you intend to manipulate it.

It looks like your homework is to maintain a collection of student records.

1
2
3
4
5
6
struct Student
  {
  string name;
  int GPA;
  ...
  };

You'll need a function to write a student record to file:

1
2
3
4
5
6
void write_student( ostream& f, const Student& student )
  {
  f << student.name << endl;
  f << student.GPA << endl;
  ...
  }

And a function to read a student record:

1
2
3
4
5
6
7
8
Student read_student( ifstream& f )
  {
  Student student;
  getline( f, student.name );
  f >> student.GPA;
  ...
  return student;
  }

Now all you need to do is load and save all students:

1
2
// The list of students
vector <Student> students;
1
2
3
4
5
6
7
// save all students
ofstream f( "students.txt" );
for (size_t n = 0; n < students.size(); n++)
  {
  write_student( f, students[ n ] );
  }
f.close();
1
2
3
4
5
6
7
8
9
10
// load all students
ifstream f( "students.txt" );
Student student;
students.clear();
while (true)
  {
  student = read_student( f );
  if (!f) break;
  students.push_back( student );
  }


Hope this helps.
thank u so much, it was useful.
Topic archived. No new replies allowed.