Reading and writing files in linux programming in C++

B

Bo Huge

Hey!

I am making a server and a client like FTP, but my own design. want
users to be able to login with usernames and pwords, which I have
stored in a file. My problem is, that i can't read from the file,
right now i have the following:

int main()
{
struct utmp current_record;
int utmpfd;
int reclen=sizeof(current_record);

if((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1)
{
perror( UTMP_FILE);
exit(1);
}
.......

And it seems to work, cause i get a value for my filedescriptor. But
how do I acces the single lines in the file?

Can I use ifstreams? (This have worked for me in windows programming
earlier, but I think there is a difference in Linux).

I would be greateful if anybody could give me some hints, or help me
understand what to do better, cause I'm pretty lost.

Thank you for your help in advance.

Y.S.

Bo
 
P

__PPS__

http://cplusplus.com/ref/cstdio/ has reference for stdio.h where you
may find out what functions you use to read from files. It's old
c-style way to do things with files.
to read files using c++:

#Include <fstream>
#include <string>
....
std::string str;
std::ifstream input (UTMP_FILE);
while(std::getline(input,str)){
//..new line read
...
//..do something with str
}
//read until the ond of file, or there was a error...
 
R

Rolf Magnus

Bo said:
Can I use ifstreams? (This have worked for me in windows programming
earlier, but I think there is a difference in Linux).

std::ifstream is a standard C++ class. There should be no noticeable
difference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top