How to retrieve data from a text file?

Pages: 123
No they are not obscure problems, they are one of the major problems with C programs, and I really don't consider throwing poor code, without any explanations, at the OP to be "acting constructively".
Grow up and do something positive in your life instead of whining. Who cares what a nobody like you ‘consider’s. Especially one who can’t understand plain English - it’s you who’s obscuring - that’s all you do.

4742 whines - 8 years of it - moan, whine, considerations - but not a line of code
@OP.

it's not a solution to copy and paste but rather to show how things could be done.
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
#include <stdio.h>
#include <windows.h>
#include <string.h>

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

const char *file_name = "records.txt";

int menu_choice();
void input_data();
void display_data();
void search_data();

int main()
{
  int choice;
  
  for(;;)
  {
    choice = menu_choice();
    switch(choice)
    {
      case 1:
        input_data();
        break;
      case 2:
        display_data();
        break;
      case 3:
        search_data();
        break;
      case 4:
				printf("Thanks for using our app.\tGood bye.");
				return EXIT_SUCCESS;
      default:
        printf("Invalid choice, please try again");
    }
  }
  return EXIT_SUCCESS;
}

int menu_choice()
{
    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 data");
    printf("\n 2. Load data");
    printf("\n 3. Search");
    printf("\n 4. Exit ");
    printf("\n\n Select: ");
    int choice;
    scanf("%d", &choice);
    
    return choice;
}

void input_data()
{
  FILE *file;
  struct Info info;
  
  file = fopen("records.txt", "a");
  if (file == NULL)
  {
    perror("Can't open records.txt");
    return;
  }
  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("%50s", info.name);
  printf("\n\n\tLast name:  ");
  scanf("%50s", info.lastname);
  printf("\n\n\tCity:       ");
  scanf("%50s", info.city);
  printf("\n\n\tCountry:    ");
  scanf("%50s", info.country);
  printf("\n\n\tPhone:      ");
  scanf("%20s", 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 display_data()
{
  struct Info info = {0};
  FILE *src = fopen(file_name, "r");
  
  if (src == NULL)
  {
    perror(NULL);
    return;
  }
  printf("--- Data in file ---\n");
  while (fscanf(src, "%50s %50s %50s %50s %20s", 
         info.name, info.lastname, info.city, info.country, info.tel) != EOF)
  {
    printf("%-15s %-15s %-15s %-15s %-s\n", 
            info.name, info.lastname, info.city, info.country, info.tel);
  }
  
}

void search_data()
{
  printf("I am only  a stub - you have to implement me.");
}


Input fie:

Anna Muzychuk Kiev UKR 000-00000
Valentina Gunina Moscow RUS 111-11111
Alexandra Kosteniuk Moscow RUS 222-22222
Elisabeth Paehtz Unknown GER 333-33333
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
#include "stdio.h"
#include "string.h"

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

void search(FILE *fp)
{
    rewind(fp);
    Address temp;
    char search_fn[25];
    int found{0};
    
    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 )
        {
            printf("*** Found: ");
            temp.display();
            printf(" ***\n");
            found = 1;
        }
    }
    if(found == 0)
        printf("No match found\n");
}

void list_all(FILE* file)
{
    rewind(file);
    Address temp;
    while( fscanf(file, "%s %s",  temp.name, temp.lastname) > 0 )
    {
        temp.display();
        printf("\n");
    }
}

void addAddress(FILE* file)
{
    char fName[25];
    printf("Please enter first name: ");
    scanf("%s", fName);
    
    char lName[25];
    printf("Please enter last name: ");
    scanf("%s", lName);
    
    Address temp;
    strcpy(temp.name, fName);
    strcpy(temp.lastname, lName);
    fprintf(file, "%s %s\n", temp.name, temp.lastname);
}

FILE* open_file()
{
    FILE* file = fopen("address_file_example.txt","r+");
    
    if(file == NULL)
    {
        printf("No file found\n");
        return NULL;
    }
    return file;
}

char menu(FILE* file)
{
    int selection{0};
    
    printf("       *** MENU ***\n");
    
    printf(" 1. Search on first name\n");
    printf(" 2. Display all addresses\n");
    printf(" 3. Add new address\n");
    
    printf(" 0. Quit and close\n\n");
    printf("    SELECT fom above: ");
    scanf("%1d", &selection);
    
    switch (selection)
    {
        case 0:
            printf("Closing ...\n");
            return 0;
        case 1:
            search(file);
            break;
        case 2:
            list_all(file);
            break;
        case 3:
            addAddress(file);
            break;
        default:
            printf("Invalid selection\n");
            break;
    }
    return selection;
}

int main()
{
    FILE* fp = open_file();
    while(menu(fp) != 0){   /* KEEP GOING */    }
    fclose(fp);
    return 0;
}
Much better, but still possible buffer overflow issues, remember you must manually reserve room for the end of string character when using the scanf() series of functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void display_data()
{
  struct Info info = {0};
  FILE *src = fopen(file_name, "r");
  
  if (src == NULL)
  {
    perror(NULL);
    return;
  }

  printf("--- Data in file ---\n");
  while (fscanf(src, "%49s%49s%49s%49s%19s", 
         info.name, info.lastname, info.city, info.country, info.tel) != EOF)
  {
    printf("%-15s %-15s %-15s %-15s %-s\n", 
            info.name, info.lastname, info.city, info.country, info.tel);
  }
  
  fclose(src);
}


Notice that the width specifier is one less than the size of the arrays.

Also note that your strings can be up to 50 characters long so it will be possible for "long" names to run together on the output. The width specifier of printf() doesn't truncate the string, it just insures that "width" (15 in this case) characters will always be printed.

Also in C you should always close file streams manually since they are not automatically closed when going out of scope.

ROFL, the whiner continues whining. Still hasn't written a line of code after 12 years of unproductive moaning. Everybody else has been constructively contributing while this jlb creep grizzles on and on like an old woman. Pegasus, the eternal nag.
There goes AgainCry, crying again because someone pointed out a problem in his code. Sheesh!

To the OP, are you required to store the data in a text file? It would be far simpler to store the Info data in a binary file. You could read/write the records with a single call to fread() / fwrite().

If it must be in a text file, I'd read the data a line at a time as I previously mentioned. Otherwise a city name like Los Angeles or New York won't scan.
And there goes grandpa when he’s over-filled his bag again. There is nothing wrong with the code - straight out of Kernigans ANSI C and totally sufficient given @OP attempt - the dope @jlb can’t even get the off by one width factor right, as unnecessary and irrelevant as it is.

All you and the useless whiner jlb do with you piffling obscurations do is confuse issues with your egoist claims of always having a better way to do things.

MYOFB grandpa and press the nurse call button, stupid, sheesh, can’t you even get that right.
Stop AgainCry. Maybe ask mommy for a juice box.
Not funny grandpa daddy diaper. Personal attacks are totally uncalled for. You should apologize.
...Personal attacks are totally uncalled for

<sarcasm>LOL, that says the right person. </sarcasm>
Nurse!!!
@dhayden,

this guy(?) needs a good caning, not orange juice.
Don't threaten me with violence shithead. You're very traceable. If you want to align yourself with grandpa hayden that's OK but watch out - you might just come unstuck with your threats.
Don't be afraid little one. I am normally peaceful and I will not hurt you. I just mentioned what you deserve. But it seems with your hyper-sensible, super-huge EGO you are punished enough.
You realise of course, @thmm, your behavior and writing pattern is archetypical, something you’ll reflect on in the future and become very concerned about.

Already you are stumbling with your word choices, even intermingling your self doubt, with outright silly, but telling, errors.

You see, you and the others Jlb initially, with dhayden and now you, have had your authority structure and self importance tested or in you case, made fun of. You are the first however, in all the time I have been doing it, who has gone so quickly beyond verbalizing your violence to cowering.

It will be more than interesting to read what you coping mechanism will now be. It’s not too difficult to predict with archetypical behavior such as yours, but I’ll let you decide.
If you give againtry attention he'll only think his life has worth :) The more of us who use this script, the faster the final meaning of his life disappears:

http://www.cplusplus.com/forum/lounge/270432/
Haha, the lame voiceover chimp has chimed in with carol’s grunt-script from wonder company Nektra.

Do you realize zapshe you have been groomed to perfection by Carol?
Ah, the sound of 'silence,' no jabbering nonsense from morons. No clamorous drone of whinging.
They can be easily brought back because I own them - just watch. groomer Carol from Nektra included
Pages: 123