How to retrieve data from a text file?

Pages: 123
Okay, Here we go again.. same code but I added the function retrieveData.
Works like for a 50%, cause it read only the first address. So If I save another address and I type the first name of that second address the program doesn't read it. How do I read every address I save in the records.txt ?

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/// Data Base
#include <stdio.h>
#include <windows.h>
#include <string.h>

void premenu();
void inputData();
void retrieveData();

struct address
{
    char name[50];
    char lastname[50];
    char city[50];
    char country[50];
    char tel[20];
}a1, a2, a3, a4, a5;

unsigned options;
FILE *file;

int main()
{
    premenu();

    for(int i = 0; i < options;)
    {
        if(options == 1)
        {
            inputData();
        }
        else if(options == 2)
        {
            retrieveData();
        }
        else if(options == 3)
        {
            printf("\n\n\t\t\t    Exiting Data Base");
            Sleep(2000);
            system("cls");
            break;
        }
        else
        {
            printf("\n\n Invalid option.. Choose 1 or 2");
            Sleep(1500);
            system("cls");
            premenu();
        }
    }
    return 0;
}

void premenu()
{
    int temp, status;
    printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
    printf("\n\n\n\tChoose an option:\n\n");
    printf("\n 1. Input address");
    printf("\n 2. Retrieve address");
    printf("\n 3. Exit Data Base");
    printf("\n\n Select: ");
    status = scanf("%d", &options);
    while(status != 1)
    {
        while((temp = getchar()) != EOF && temp != '\n');
        {
            printf("\n\n Invalid option.. Choose 1 or 2");
            Sleep(1500);
            system("cls");
            printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
            printf("\n\n\n\tChoose an option:\n\n");
            printf("\n 1. Input address");
            printf("\n 2. Retrieve address");
            printf("\n 3. Exit Data Base");
            printf("\n\n Select: ");
            status = scanf("%d", &options);
        }
    }
}

void inputData()
{
    file = fopen("records.txt", "a");

    if(!file)
    {
        printf("\n File could not be opened\n\n"
               " make sure the file exists"
               "\n\n or the file is not read only\a");
        Sleep(3500);
        system("cls");
        premenu();
    }
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       ");
        scanf("%s", a1.name);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  ");
        scanf("%s", a2.lastname);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  %s", a2.lastname);
        printf("\n\n\tCity:       ");
        scanf("%s", a3.city);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  %s", a2.lastname);
        printf("\n\n\tCity:       %s", a3.city);
        printf("\n\n\tCountry:    ");
        scanf("%s", a4.country);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  %s", a2.lastname);
        printf("\n\n\tCity:       %s", a3.city);
        printf("\n\n\tCountry:    %s", a4.country);
        printf("\n\n\tPhone:      ");
        scanf("%s", a5.tel);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  %s", a2.lastname);
        printf("\n\n\tCity:       %s", a3.city);
        printf("\n\n\tCountry:    %s", a4.country);
        printf("\n\n\tPhone:      %s", a5.tel);

        fprintf(file, "# Name:\t%s   %s\n  City:\t%s\n  Country:"
                "   %s\n  Phone:      %s\n",
                a1.name, a2.lastname, a3.city, a4.country, a5.tel);

        fclose(file);

        printf("\n\n\n Contact saved to ledger\n\n"
               " ..returning to main menu");
        Sleep(2500);
        system("cls");
        premenu();
}

void retrieveData()
{
    int found = 0;
    char tmp[50];

    file = fopen("records.txt", "r");

    if(!file)
    {
        printf("\n File not found\n\n"
               " make sure the file exists\a");
        Sleep(3500);
        system("cls");
        premenu();
        return;
    }

    system("cls");
    printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
    printf("\n\n Search address");
    printf("\n\n\n\tName:       ");
    scanf("%s", tmp);

    if(!feof(file))
    {
        fscanf(file, "# Name:\t%s   %s\n  City:\t%s\n  Country:"
                "   %s\n  Phone:      %s", a1.name, a2.lastname, a3.city, a4.country, a5.tel);
        if(strcmp(tmp, a1.name) == 0)
        {
            printf("\n\n Address found:\n\n  Last name:  %s\n  City:\t      "
                   "%s\n  Country:    %s\n  Phone:      %s\n\n",
                    a2.lastname, a3.city, a4.country, a5.tel);
            found = 1;
            Sleep(7000);
            printf("\n\n ..returning to main menu");
            Sleep(3000);
            system("cls");
        }
    }
    if(!found)
    {
        printf("\n\n Address not found\n\a");
        Sleep(2500);
        printf("\n\n ..returning to main menu");
        Sleep(2000);
        system("cls");
    }

    fclose(file);
    premenu();
}
Last edited on
Well, the short answer to your question is you need to incorporate overloaded stream >> ( and << ) operators in your struct.

So, you end up with file >> address; in some sort of a loop instead your 'code'.


https://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/

RetrieveData() needs to read through all the records, looking for the requested one. It currently checks only the first record.

Why do you store the headings, like "Name:" in the data file? They just take up space. Why not store just the fields, one per line.

Your program won't work for a first name like "Jean Louis" or a city like "Los Angeles"
Hello mif,

There are many things wrong with your program and some places that can be improved on.

To start with:
1
2
3
4
5
6
7
8
struct address
{
    char name[50];
    char lastname[50];
    char city[50];
    char country[50];
    char tel[20];
}a1, a2, a3, a4, a5;

"address" is a misleading name for the struct as there is no address in the struct. Consider "Person" or "Info".

"a1" - "a5" creates 5 objects of the struct, but you only need 1. By storing each variable in a different struct it tends to defeat the purpose of the struct. You could just as easily create the 5 variables without the struct. The struct would make more sense as:
1
2
3
4
5
6
7
8
struct Person
{
    char name[50];
    char lastname[50];
    char city[50];
    char country[50];
    char tel[20];
}person;


It is best to avoid the global variables:
1
2
unsigned options;
FILE *file;

"options" would work better defined in "main" and passed to a function or the function could return a value captured by "options".

The "FILE*" should be defined in the functions that need it and when the function ends so will the variable.

In "main" lines 24 - 49 would work better in a do/while loop, so that it keeps repeating until you choose "3".

The for loop make no sense. Since "i" never changes it will always be less than "options". Also using a do/while loop will eliminate the need for the for loop.

In the "premenu" function you are duplicating code that you do not need to. A do/while loop would work better here and only need to print the menu once.

The function could either return a valid choice or you could pass "options" by reference and use it in the function.

In the function "inputdata" start with FILE* file;. When the function ends so will this variable.

In the if statement the first line of text is OK, but line 89 is irrelevant as opening the file with modes "w", "w+", "a" or "a+" will create the file if it does not exist And line 90 should read "\n\n or the file is read only\a\n");. Remove "not", but this is only true if you have changed the file status to "read only". The open statement does not set the file to read only.

All the "scanf" statements should be to "a1.something" or to "person.something" to properly use the struct.

Line 133 is outputting more information than you need making it more difficult to read the file. You just need to output each variable of the struct making the output file more like:
First
Last
City
Country
Phone
First
Last
City
Country
Phone


This will make it much easier to read the file not having all that extra information.

In the "retrieveData" function. the variable found could be a bool and work the same way.

I see that you change some of the code. In the if statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (!file)
{
    printf("\n File not found\n\n"
            " make sure the file exists\a");

    Sleep(3500);

    system("cls");

    premenu();

    return;
}

Remove line 10. This should be done in "main" not here.

if (!feof(file)). By the time this becomes false you will have tried to read something with a file stream that no longer works. Meaning that you will try to process a record that was not read.

I have not tested it yet, so I do not know if the "fscanf" function works, but you are doing more work than is needed.

That is what I see for now.

Andy
Yes Andy the code works very well... AND another YES for your comments.. as I read and follow your explanations above, I realize I have done a lots of mistakes, and I will make some changes in my code.
I will stop for a moment and modify my code as you mentioned before, but I would be grateful if some of you guys maybe if you have a small space in your life to help me after with my problem that was mentioned before.

Thank you all !!
Almost done.. Still Andy in the retrieveData Function, I don't have to remove the premenu() and add it in the "main", just because if I choose 2 for the first time when program starts is nothing to worry about but if I change like you said when task 2 ends it goes to premenu() function and the second time you choose 2 the premenu() will repeat and print 2 times the same thing on the screen. I don't like to see things repeating on the console so I tested and works very well..
And the if statement from the inputData I removed completely just because like you said the open statement doesn't set to read only nor delete the file.. so:

1.File could not be opened
2.make sure the file exists
3.or the file is read only

non of this is relevant because choosing 1 from the main menu always crate an empty file not read only so if the 2 and 3 is irrelevant then 1 is non sens as well.
Last edited on
Hello mif,

You should post your revised code so I can see what you have done.

I did get it to work the way I was thinking and was even able to remove some redundant code but I am having a problem with dhayden suggestion if having unformatted input for "name" and "city" and maybe even "phone" to keep spaces in the string if there.

Andy
if you have a small space in your life to help me after with my problem that was mentioned before.
I already did:
RetrieveData() needs to read through all the records, looking for the requested one. It currently checks only the first record.


Andy, the key is to read the input as a line. scanf("%s", ...) will read a single word.
@dhayden,

Yes I understand that and I came to a better understanding of this after the reading I did.

I was thinking that "vscanf" would work, but I had problems with this and have yet to figure it out.

It has been over 20 years since I have worked with C, so I am a bit rusty with some parts and functions.

I know what is needed, I have not figured it out yet.

Andy
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
#include <iostream>
#include <fstream>

struct Address
{
    char name[50];
    char lastname[50];
    
    void display()
    {
        std::cout << name << '-' << lastname;
    }
    
    //    friend std::ostream& operator<< (std::ostream& out, Address& rhs)
    //    {
    //        out << rhs.name << '-' << rhs.lastname;
    //        return out;
    //    }
};

int main()
{
    std::ifstream fin ("address.txt");
   
    if (!fin.is_open())
    {
        std::cout << "Unable to open file\n";
        return -1;
    }
    
    // READ ALL THE ADDRESSES FROM FILE
    Address temp;
    while( fin >> temp.name >> temp.lastname)
    {
        temp.display();
        std::cout << '\n';
        
        //        std::cout << temp << '\n'; // GOES WITH operator<<
    }
    
    // SEARCH FILE ON firstname
    char search_fn[]{"Julie"};
    
    fin.clear();
    fin.seekg(0);
    
    while( fin >> temp.name >> temp.lastname)
    {
        if( strcmp( search_fn,temp.name) == 0)
        {
            std::cout << "*** ";
            temp.display();
            std::cout << '\n';
        }
    }
    fin.close();
    
    
    // ADD Address TO FILE
    Address somebody;
    strcpy(somebody.name, "SomeName");
    
    std::ofstream fout;
    fout.open("address.txt", std::ios::app);
    fout << somebody.name << ' ' << "lastname" << '\n';
    fout.close();

    return 0;
}


Bob Smith
Julie Brown
Malcolm Green
Red Presley
SomeName lastname
Okay.. here is my revised code Andy, without making any progress with the retrieveData function, as againtry said:
Andy, the key is to read the input as a line
, which I'm working on, and btw, I'm working with C not C++ so as a beginner I think I recognize the haders #include <iostream> and #include <fstream> which is C++

here is what I've changed:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/// Data Base
#include <stdio.h>
#include <windows.h>
#include <string.h>

void inputData();
void retrieveData();

struct Info
{
    char name[50];
    char lastname[50];
    char city[50];
    char country[50];
    char tel[20];
}info;

void text()
{
    printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
    printf("\n\n\n\tChoose an option:\n\n");
    printf("\n 1. Input info");
    printf("\n 2. Retrieve info");
    printf("\n 3. Exit Data Base");
    printf("\n\n Select: ");
}

int main()
{
    int temp, status;
    unsigned options;
    do
    {
        text();
        status = scanf("%d", &options);
        while(status != 1)
        {
            while((temp = getchar()) != EOF && temp != '\n');
            {
                printf("\n\n Invalid option.. Choose 1, 2 or 3");
                Sleep(2000);
                system("cls");
                text();
                status = scanf("%d", &options);
            }
        }

        if(options == 1)
        {
            inputData();
            main();
        }
        else if(options == 2)
        {
            retrieveData();
        }
        else if(options == 3)
        {
            printf("\n\n\t\t\t    Exiting Data Base");
            Sleep(2000);
            system("cls");
        }
        else
        {
            printf("\n\n Invalid option.. Choose 1, 2 or 3");
            Sleep(2000);
            system("cls");
        }
    }while(options != 3);

    return 0;
}

void inputData()
{
    FILE *file;
    file = fopen("records.txt", "a");

    system("cls");
    printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
    printf("\n\n Enter info");
    printf("\n\n\n\tName:       ");
    scanf("%s", info.name);
    printf("\n\n\tLast name:  ");
    scanf("%s", info.lastname);
    printf("\n\n\tCity:       ");
    scanf("%s", info.city);
    printf("\n\n\tCountry:    ");
    scanf("%s", info.country);
    printf("\n\n\tPhone:      ");
    scanf("%s", info.tel);

    fprintf(file, "%s %s %s %s %s\n", info.name, info.lastname, info.city, info.country, info.tel);

    fclose(file);

    printf("\n\n\n Contact saved to ledger\n\n"
           " ..returning to main menu");
    Sleep(2500);
    system("cls");
}

void retrieveData()
{
    FILE *file;
    int found = 0;
    char tmp[50];

    file = fopen("records.txt", "r");

    if(!file)
    {
        printf("\n File not found\n\n"
               " make sure the file exists\a");
        Sleep(3500);
        system("cls");
        return;
    }

    system("cls");
    printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
    printf("\n\n Search info");
    printf("\n\n\n\tName:       ");
    scanf("%s", tmp);

    if(!feof(file))
    {
        fscanf(file, "%s%s%s%s%s", info.name, info.lastname, info.city, info.country, info.tel);
        if(strcmp(tmp, info.name) == 0)
        {
            printf("\n\n Info found:\n\n  Last name:  %s\n  City:\t      "
                   "%s\n  Country:    %s\n  Phone:      %s\n\n",
                    info.lastname, info.city, info.country, info.tel);
            found = 1;
            Sleep(7000);
            printf("\n\n ..returning to main menu");
            Sleep(3000);
            system("cls");
        }
    }
    if(!found)
    {
        printf("\n\n Info not found\n\a");
        Sleep(2500);
        printf("\n\n ..returning to main menu");
        Sleep(2000);
        system("cls");
    }
    fclose(file);
}
Can you post the input file - or some lines of it in case it's too long.
Hi Thomas.. I can show you everything, but I need to ask you what do you mean by showing the input file ? Sorry for asking you this but I don't understand your questions.
Did you meant the content of the text file, few lines of the data is stored or.. ?
Add fflush(stdout); before each call to Sleep() to ensure that whatever you've printed will actually get displayed before the call to Sleep().

Lines 38-45 may not work as you intended. The while loop has no body because line 38 ends with a semicolon. Then you have a block of code that executes exactly once. I'd replace all of it with:

1
2
3
4
5
6
7
8
9
10
11
12
        // Read and discard the rest of the line
        while((temp = getchar()) != EOF && temp != '\n')
            ;      // Put the semicolon on a separate line to make it clear that this is an empty body
        if (status != 1) {

            printf("\n\n Invalid option.. Choose 1, 2 or 3");
            fflush(stdout);
            Sleep(2000);
            system("cls");
            options = 0;        // to ensure the result of the loop test below
            continue;           // jump to the loop test
        }


Line 51: It's illegal to call main() within the program (at least it is in C++). Just remove this line and let the loop handle the logic.

Lines 113 & 114: The error message should be more informative. What file can't be found? In fact, are you sure that the file doesn't exist? There are many other reasons why fopen might fail. Fortunately, there's an easy way to get the details. fopen() (along with most of the rest of the C library) sets the global variable errno to indicate the reason for the failure. There's a handy function called strerror() that converts an errno value into english text. So, I'd change lines 113 and 114 to:
printf("\n Can't open records.txt: %s\n\n", strerror(errno));

Okay, this leaves us with the original problem: reading multiple records in retrieveData(), and the one I mentioned where you read a single word instead of a whole line.

You can use fgets() to read a line of text, but be aware that it leaves the newline in the string that it returns. However, if the file ends without a newline, then the last line read won't have it. Ugh. For these reasons if I have to use fgets(), I usually write a wrapper around it:
1
2
3
4
5
6
7
8
9
10
11
12
// Like fgets(), but this removes the optional newline.
char *getString(char *str, int num, FILE *stream)
{
    char *result = fgets(str, num, stream);
    if (result) {
        int len = strlen(str);
        if (len && str[len-1] == '\n') {
            str[len-1] = 0;
        }
    }
    return result;
}


Then you can call it like this, for example, to read the name field of an info record from stdin:
getString(info.name, sizeof(info.name), stdin);

When reading/writing data to/from a file, it's usually a good idea to write the code as a matching pair of functions (or C++ methods or operators). So for reading/writing to the file, I created two functions:
1
2
3
4
5
6
7
8
9
10
11
// Write the Info record to the file. Returns 0 on success, non-zero on error
int writeInfo(Info *info, FILE *file)
{
    // PUT YOUR CODE HERE
}

// Read an Info record. Return 0 on success, non-zero on error.
int readInfo(Info *info, FILE *file)
{
    // PUT YOUR CODE HERE
}

The idea here is that if you write a record with writeInfo(), then readInfo() should be able to read it and should read every byte that writeInfo() wrote. Be sure to read and write the fields as separate lines so you can handle fields with spaces in them.

Finally we come to the original problem: you're only reading one record in retrieveData(). The reason should be obvious: there's no loop in retrieveData()! The middle part of the function should look like this:
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
    ...
    system("cls");
    printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
    printf("\n\n Search info");
    printf("\n\n\n\tName:       ");
    getString(tmp, sizeof(tmp), stdin);

    while (readInfo(&info, file) == 0)
    {
        if(strcmp(tmp, info.name) == 0)
        {
            printf("\n\n Info found:\n\n  Last name:  %s\n  City:\t      "
                   "%s\n  Country:    %s\n  Phone:      %s\n\n",
                    info.lastname, info.city, info.country, info.tel);
            found = 1;
            fflush(stdout);
            Sleep(7000);
            printf("\n\n ..returning to main menu");
            fflush(stdout);
            Sleep(3000);
            system("cls");
        }
    }
    if(!found)
    ...

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
#include "stdio.h"
#include "string.h"

struct Address
{
    char name[50];
    char lastname[50];
    
    void display(){ printf( "%s - %s", name, lastname); }
};

int main()
{
    // SETUP FILE FOR INPUT & OUTPUT
    
    FILE* fp = fopen("address_file_example.txt","r+");
    
    if(fp == NULL)
    {
        printf("No file found\n");
        return -1;
    }
    
    // READ ALL THE ADDRESSES FROM FILE
    Address temp;
    while( fscanf(fp, "%s %s",  temp.name, temp.lastname) > 0 )
    {
        temp.display();
        printf("\n");
    }

    // SEARCH FILE ON firstname
    char search_fn[25];
    char name[25];
    char lastname[25];
    
    int found = 0;

    // REWIND FILE TO START
    fseek( fp, 0, SEEK_SET );

    printf("Please enter a first name: ");
    scanf("%s", search_fn);
    
    while( fscanf(fp, "%s %s\n",  name, lastname) > 0 )
    {
        if( strcmp( search_fn, name ) == 0 )
        {
            printf("*** Found: %s %s ***\n", name, lastname);
            found = 1;
        }
    }
    if(found == 0)
        printf("No match found\n");

    // ADD Address TO FILE
    Address somebody;
    char some_Fname[]{"Harry"};
    char some_Lname[]{"Houdini"};
    strcpy(somebody.name, some_Fname);
    strcpy(somebody.lastname, some_Lname);
    fprintf(fp, "%s %s\n", somebody.name, somebody.lastname);
    
    // CLOSE FILE
    fclose(fp);

    return 0;
}
Bob - Smith
Julie - Brown
Malcolm - Green
Red - Presley
Harry - Houdini
Please enter a first name: Harry
*** Found: Harry Houdini ***
Program ended with exit code: 0
Or you can make use of the struct display() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// REWIND FILE TO START
    fseek( fp, 0, SEEK_SET );

    printf("Please enter a first name: ");
    scanf("%s", search_fn);
    
    while( fscanf(fp, "%s %s\n",  temp.name, temp.lastname) > 0 )
    {
        if( strcmp( search_fn, temp.name ) == 0 )
        {
            temp.display();
            printf("\n");
            
            //printf("*** Found: %s %s ***\n", name, lastname);
            found = 1;
        }
    }
    if(found == 0)
        printf("No match found\n");
Last edited on
@OP,
Did you meant the content of the text file, few lines of the data is stored or.. ?

yes exactly.
1
2
3
4
    scanf("%s", search_fn);
    
    while( fscanf(fp, "%s %s\n",  temp.name, temp.lastname) > 0 )
    {


Do you realize that the above code is full of buffer overflows waiting to happen?

Never use a method that doesn't limit the number of characters allowed to be retrieved. The scanf() series of functions can be limited using the proper "optional" width specifiers.

Also what happens if there are whitespace characters in the strings?


@jlb
Boring - I’m well aware of that - they’re the least of @OP’s problems and only obscure the issues. Try and act constructively. Everybody else is.
Pages: 123