Displaying files through structures and functions

Iv only started a Persistent Data course and im struggling. I have to display customer and sales details.. but unsure what way to go about it.
Here is my layout so far..
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct customer
{
   int id;
   char first_name[40];
   char surname[40];
   int age;
   char gender[7];
   char address[100];
};

struct sales
{
   int id;
   char product_description[100];
   float price;
   int quantity;
};

struct customer new_customer;
struct sales new_sale;

void display_cust(void);
void display_sale(void);

main()
{
  FILE* fp;

  fp=fopen("customers.txt","rb");

  if(!fp)
  {
    printf("unable to open file\n");
  }
  
  int num1;
  int num2;

  printf("Please enter 1 to display a customer\n2 to display a sale\n"); 
  scanf("%5d", &num1, &num2);
  
  if(num1)
  {
	  display_cust();
  }
  else if(num2)
  {
	  display_sale();
  }
  else
  {
	  printf("Could not find match\n");
  }
  fclose(fp);
  
  getchar();
  flushall();

  return 0;
}
void display_cust(void)
{
  fp=fopen("customers.txt","rb");

  if(!fp)
  {
    printf("unable to open file\n");
  }
  
}

void display_sale(void)
{
  fp=fopen("sales.txt","rb");

  if(!fp)
  {
    printf("unable to open file\n");
  }
}
I would include a variable named customer_id, and place it in your sales struct. Every sale will have the customer_id variable populated with the id of the customer who made that purchase. It's called a foreign key. This way, you can correlate the purchases with the customers who made them. This is common in databases and the concepts works the same here.

Also you open customers.txt in main() needlessly because you have it set to open when the user selects 1 as well.

I'm not sure what your main issue is, if you are trying to display the lines in the file, you can use getline();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void display_cust(void)
{
  string line;
  fp=fopen("customers.txt","rb");

  if(!fp)
  {
    printf("unable to open file\n");
  } else {
      while (fp.good())
      {
          getline(fp, line, '//delimiter goes here//');
          cout << line << endl;
       }
   }
}
Last edited on
Cheers, and i suppose that code would work for the sales as well(Slightly modified obviously)
What does cout << line << endl; do??
Im basically just trying to search the customers and sales then displaying them..
make sure to include #include <iostream> at the top of your file, this will allow you to use cout, cin, etc.... in your file.

cout = console output
<< line = output the variable 'line's contents to the console
<< endl = make a newline in the console.

cout << line << endl would display each field in the text file, where getline(fp, line, ' ') would read each piece of text into the string variable 'line', between the spaces in the line of text in the file.

If your customers file looked like this:
1,bob,smith,27,male,123 foo lane tampa fl,
2,jane,smith,23,female,123 foo lane tampa fl

then you would use
 
getline(fp, line, ',')

instead, because a comma is your delimiter, and each pass through the while loop will read the line of text within the two commas and put that into the string variable 'line'
Last edited on
Ok thats great thanks, and by any chance would you know how to add customer and sale details?
Same idea, except your not reading the file anymore your writing to it. You need to read the data for each label into variables first.

I'm more familiar with ifstream and ofstream not fopen but
i.e...
1
2
3
4
5
6
7
8
string firstname;
string surname;
fp=fopen("customers.txt","rb");

fputs (firstname+",", fp);
fputs (surname+",", fp);
fputs ("\n", fp);  // end of record(or row)
Last edited on
Do you not use "wb" instead of "rb"??
And for entering an id number would i use fgets ((id));
instead of fputs..
Last edited on
Yes write file, wb. sorry.
for this code
string line;
fp=fopen("sales.txt","rb");

if(!fp)
{
printf("unable to open file\n");
} else {
while (fp.good())
{
getline(fp, line, ',');
cout << line << endl;
}

what do i replace string line with?
Nothing. You can think of string line as a temporary box that holds items for each iteration of the while loop.

so if your customer file contains:
1,bob,smith,27,male,123 foo lane tampa fl,


than your code will be doing this:

1
2
3
4
5
while (fp.good())
{
getline(fp, line, ',');
cout << line << endl;
}


produces:


while (determined your file has more to read)
1 // this is line after 1st iteration of while loop
while (determined your file has more to read)
bob // this is line after 2nd iteration of while loop
while (determined your file has more to read)
smith // this is line after 3rd iteration of while loop
while (determined your file has more to read)
27  // etc..
while (determined your file has more to read)
male // etc..
while (determined your file has more to read)
123 foo lane tampa fl // etc..
while (determined your file has NO more to read)


I have to go to work, everything here is enough to get you going. Remember, this web-site has a complete and very useful reference for everything C++ as well as tutorials: http://www.cplusplus.com/reference/ and google is your friend.
Last edited on
Ok cheers!!
I worked away with the code and got 2 functions done so far, there's no errors but im still not 100% on it.. Anyways i just have 2 functions left(Adding sales and customers) Heres what iv got so far..

[CODE]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct customer
{
int id;
char first_name[40];
char surname[40];
int age;
char gender[7];
char address[100];
};

struct sales
{
char customer_id;
int id;
char product_description[100];
float price;
int quantity;
};

struct customer new_customer;
struct sales new_sale;

void search(void);
void binary(void);
void add_customer(void);
void add_sale(void);
main()
{
FILE* fp;

fp=fopen("customers.txt","rb");

if(!fp)
{
printf("unable to open file\n");
}

int num1, num2, num3, num4;

printf("Please enter\n1. to search a customer or sale\n2. to add a customer\n3. to add a sale\n4. binary search\n");
scanf("%5d", &num1, &num2, &num3, &num4);

if(num1)
{
search();
}
else if(num2)
{
add_customer();
}
else if(num3)
{
add_sale();
}
else if(num4)
{
binary();
}
else
{
printf("Could not find match\n");
}
fclose(fp);

return 0;
}

void search(void)
{
FILE *fpcust;
int i, len_string;
char searchID [30];
char temp[30];

printf("What do you want to search for? \n");
scanf(" %s", searchID);

fpcust = fopen ("c:\\presdatatest.txt", "r");
rewind(fpcust);

if ( fpcust == NULL)
{
printf("File cound not be opened.");
exit(0);
}

else
{
len_string = strlen(searchID);

while (!feof ( fpcust ) )
{
for (i = 0; i < len_string; i++)
{
temp[i] = fgetc ( fpcust );
}
temp[i] = '\0';

//strcmp used for comparing both strings
if ( strcmp ( searchID, temp ) == 0)
{
printf("The ID was found \n");
fclose( fpcust);
system ("PAUSE");
exit(1);
}

else
{
printf("No matches found. \n");
system ("PAUSE");
exit (1);
}

fseek ( fpcust, - (len_string - 1), 1);
}
fclose ( fpcust );
}

}

void binary (void)
{

//creating biary copy of text file for customers
FILE *fpcust, *fpcustbin;
char buffer;
int ch;
int ch1;


//pointers pointing to files on the computer
fpcust = fopen("c:\\customers.txt", "r");
fpcustbin = fopen("c:\\customers.bin", "wb");


//creating binary copy of text file for customers
while ( ( ch = fgetc ( fpcust ) ) != EOF )
{
fread(&buffer, 1, 1, fpcust);
fwrite(&buffer, 1, 1, fpcustbin);
}


//closing the customer files
fclose(fpcust);
fclose(fpcustbin);


//creating binary copy of text file for sales
FILE *fpsales, *fpsalesbin;
char buffer1;


//pointers pointing to files on the computer
fpsales = fopen("c:\\sales.txt", "r");
fpsalesbin = fopen("c:\\sales.bin", "wb");


//copying text from sales text file into binary file
while ( ( ch1 = fgetc ( fpsales ) ) != EOF )
{
fread(&buffer1, 1, 1, fpsales);
fwrite(&buffer1, 1, 1, fpsalesbin);
}


//cosing the sales files
fclose(fpsales);
fclose(fpsalesbin);

}//end to binary function

void add_customer(void)
{

}

void add_sale(void)
{

}
[CODE]
[CODE]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct customer
{
int id;
char first_name[40];
char surname[40];
int age;
char gender[7];
char address[100];
};

struct sales
{
char customer_id;
int id;
char product_description[100];
float price;
int quantity;
};

struct customer new_customer;
struct sales new_sale;

void search(void);
void binary(void);
void add_customer(void);
void add_sale(void);
main()
{
FILE* fp;

fp=fopen("customers.txt","rb");

if(!fp)
{
printf("unable to open file\n");
}

int num1, num2, num3, num4;

printf("Please enter\n1. to search a customer or sale\n2. to add a customer\n3. to add a sale\n4. binary search\n");
scanf("%5d", &num1, &num2, &num3, &num4);

if(num1)
{
search();
}
else if(num2)
{
add_customer();
}
else if(num3)
{
add_sale();
}
else if(num4)
{
binary();
}
else
{
printf("Could not find match\n");
}
fclose(fp);

return 0;
}

void search(void)
{
FILE *fpcust;
int i, len_string;
char searchID [30];
char temp[30];

printf("What do you want to search for? \n");
scanf(" %s", searchID);

fpcust = fopen ("c:\\presdatatest.txt", "r");
rewind(fpcust);

if ( fpcust == NULL)
{
printf("File cound not be opened.");
exit(0);
}

else
{
len_string = strlen(searchID);

while (!feof ( fpcust ) )
{
for (i = 0; i < len_string; i++)
{
temp[i] = fgetc ( fpcust );
}
temp[i] = '\0';

//strcmp used for comparing both strings
if ( strcmp ( searchID, temp ) == 0)
{
printf("The ID was found \n");
fclose( fpcust);
system ("PAUSE");
exit(1);
}

else
{
printf("No matches found. \n");
system ("PAUSE");
exit (1);
}

fseek ( fpcust, - (len_string - 1), 1);
}
fclose ( fpcust );
}

}

void binary (void)
{

//creating biary copy of text file for customers
FILE *fpcust, *fpcustbin;
char buffer;
int ch;
int ch1;


//pointers pointing to files on the computer
fpcust = fopen("c:\\customers.txt", "r");
fpcustbin = fopen("c:\\customers.bin", "wb");


//creating binary copy of text file for customers
while ( ( ch = fgetc ( fpcust ) ) != EOF )
{
fread(&buffer, 1, 1, fpcust);
fwrite(&buffer, 1, 1, fpcustbin);
}


//closing the customer files
fclose(fpcust);
fclose(fpcustbin);


//creating binary copy of text file for sales
FILE *fpsales, *fpsalesbin;
char buffer1;


//pointers pointing to files on the computer
fpsales = fopen("c:\\sales.txt", "r");
fpsalesbin = fopen("c:\\sales.bin", "wb");


//copying text from sales text file into binary file
while ( ( ch1 = fgetc ( fpsales ) ) != EOF )
{
fread(&buffer1, 1, 1, fpsales);
fwrite(&buffer1, 1, 1, fpsalesbin);
}


//cosing the sales files
fclose(fpsales);
fclose(fpsalesbin);

}//end to binary function

void add_customer(void)
{

}

void add_sale(void)
{

}
[CODE]
Topic archived. No new replies allowed.