I need help with writing and reading a txt file

Hi guys I have problem with putting a multiple data to txt file.

For exemple I show you what I want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>  

struct user{
char Name[256];
char Pass[7];
int  ID;
}user1;

int main(){

printf ("Set name: ");
scanf ("%s",user1.Name)//I initialize as "QIZI"

printf ("Set password: ");
scanf ("%s",user1.Pass);//I initialize as "12345"

printf ("Set your's id: ");
scanf ("%s",user1.ID) //I initialize as 2

}


I have now initialized three variables, i want put in txt file like this:

QIZI
12345
2



Then I need get back that writen variables to struct user (from the txt file).

Can somebody help me ?
Last edited on
Since they are line-by-line and not delimited you can use:

http://www.cplusplus.com/reference/iostream/istream/getline/

Thanks for answer, but I need some alternative way in stdio.h .
I think fgets() does what you want. It stops when it hits a newline or max chars have been read just like getline:

http://www.cplusplus.com/reference/clibrary/cstdio/fgets/
Ok, thanks it work.
Topic archived. No new replies allowed.