FILE

lads, I need to extract from initial declared arrays n[100],p[100],g[100] using fgetc();( each character), into the new ones nn[i]. pp[i] and gg[i], check the code below pls and help me to finish it if possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int main()
{    
    FILE *fp;
    
    char NF[100];
    
    cout<<" Enter file name = ";
    cin>>NF;
    
    strcat(NF,".step"); // a random extension to the file. 

    fp = fopen("C:\\LPS2025ro.txt", "w+"); // my purpose was to save it to disk C
    
    if( fp == NULL ) {  cout<<" error W+ "<<endl; exit(0);  }
     else {  cout<<" OK w+ "<<endl; }
     
     char n[100], p[100], g[100];
     
     cout<<" Enter name::";
     cin>>n;
     cout<<" Enter surname::";
     cin>>p;
     cout<<" Enter group::";
     cin>>g;
     
       for(int i=0; i< strlen(n); i++)
       fputc(n[i],fp);
       
       fputc('\n',fp); //a random symbol to distinguish name,surname, group
       
        for(int i=0; i< strlen(p); i++)
       fputc(p[i],fp);
       
       fputc('\n',fp); // 
       
        for(int i=0; i< strlen(g); i++)
       fputc(g[i],fp);
       
 fclose(fp);
 
    fp = fopen(NF, "r+");
    if( fp == NULL ) {  cout<<" error R+ "<<endl; exit(0);  }
     else {  cout<<" OK R+ "<<endl; }
     
      char c, nn[100], pp[100], gg[100];
      
      while( !feof(fp) )
      {
        c = fgetc(fp);
       cout<<" c="<<c;
        if( c == '\n' ) cout<<" \\n "<<endl;
         else cout<<endl;
   }
    fclose(fp);
 return 0;
}
Your indentation is a mess.

Is there a reason you variables are called n, p, g instead of name, surname, group?
Your indentation is a mess.

Wow! Another gem of administrator wisdom.
Why the mixture of C++ code with C file streams??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	char NF[100] {};

	cout << "Enter file name = ";
	cin >> NF;

	strcat(NF, ".step"); // a random extension to the file.

	//FILE* fp = fopen("C:\\LPS2025ro.txt", "w+"); // my purpose was to save it to disk C
	FILE* fp = fopen(NF, "w");

	if (fp == NULL) {
		cout << "Error W " << endl;
		return 1;
	}

	cout << "OK w " << endl;

	char n[100] {}, p[100] {}, g[100] {};

	cout << "Enter name::";
	cin >> n;

	cout << "Enter surname::";
	cin >> p;

	cout << "Enter group::";
	cin >> g;

	for (int i = 0; i < strlen(n); i++)
		fputc(n[i], fp);

	fputc('\n', fp);

	for (int i = 0; i < strlen(p); i++)
		fputc(p[i], fp);

	fputc('\n', fp);

	for (int i = 0; i < strlen(g); i++)
		fputc(g[i], fp);

	fputc('\n', fp);

	fclose(fp);

	fp = fopen(NF, "r");

	if (fp == NULL) {
		cout << "Error R " << endl;
		return 2;
	}

	cout << "OK R " << endl;

	char nn[100] {}, pp[100] {}, gg[100] {};

	for (int n = 0, c; (c = fgetc(fp)) != EOF && c != '\n'; nn[n++] = c);
	for (int n = 0, c; (c = fgetc(fp)) != EOF && c != '\n'; pp[n++] = c);
	for (int n = 0, c; (c = fgetc(fp)) != EOF && c != '\n'; gg[n++] = c);

	cout << "nn: " << nn << '\n';
	cout << "pp: " << pp << '\n';
	cout << "gg: " << gg << '\n';

	fclose(fp);
	return 0;
}

Topic archived. No new replies allowed.