Jump to content

c++ file read

- - - - -

  • Please log in to reply
3 replies to this topic

#1
sh4

sh4

    Newbie

  • Members
  • PipPip
  • 16 posts
Hello all

i have a question i didnt make it please help me,

For example there is file name is myfile.txt
my name is eric
my age is 20
our company name is Goggle
********
...

there is many lines.how can i read frst line data and equal to str[100] or something
and delete a readed line in a file?
i am waiting for help
Thank you

#2
adash

adash

    Newbie

  • Members
  • Pip
  • 6 posts
maybe the simplest way is to use std::vector of strings to read the whole file and then operate on the vector of strings and do whatever you want.
For instance to read the file line by line :


#include <vector>

#include <string>

#include <fstream>


[.......]


vector<string> temp;

fstream inp_file("myfile.txt");


while(!inp_file.eof()) 

    { 

    getline(inp_file,st); 

    temp.push_back(st); 

    } 

inp_file.close();


In the end you get vector with all the lines inside.
I am not sure if it is what you expect...

#3
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

The above code should help you with what you're looking for. After you have read all the lines from file: you can delete the file OR re-open file with CREATE file flag.

Munir

#4
sh4

sh4

    Newbie

  • Members
  • PipPip
  • 16 posts
Thanks for your help munir

i solved the problem in my hard way. at last.

Erkin,




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users