Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are to complete this code to read the company names from a database and print

ID: 3860752 • Letter: W

Question

We are to complete this code to read the company names from a database and print them into a text file. I am not sure that it will run, because I stuck on the printInfo part. I am not sure I am putting it in correctly. Please check the entire program. Thank you very much. Here is what I have...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// set string lengths
#define STRSHORT 15
#define STRMED 30
#define STRLONG 80
using namespace std; // created as a C++ CodeBlocks project

typedef struct company
{
char name [STRLONG];
char street [STRMED];
char city [STRMED];
char state [STRMED];
char zip [STRSHORT];
char phone [STRSHORT];
char cell [STRSHORT];
int rating;
char contact [STRLONG];
}company;

void printInfo(company* info, FILE *fp);

int main()
{
FILE *fin;
FILE *fout;
int BlockNumber;
char store[STRLONG]; //create an input file name (think up a name)
char plumbing[STRLONG]; //create an output file name

company* info= new company;

printf("Enter input file name :");
scanf("%s", store );
fflush(stdin); /* flush keyboard buffer */

// open binary data file
if ((fin = fopen("pipes.bin", "rb")) ==NULL)
{
fprintf(stderr, "Error opening input file.");
exit(1);
}

printf("Enter output file name :");
scanf("%s", plumbing);
fflush(stdin); /* flush keyboard buffer */

// open output text file
if ((fout = fopen ("C:/TEMP/plumbing.txt", "w+"))==NULL)
{
fprintf(stderr, "Error opening output file.");
exit(1);
}
for (BlockNumber=0; BlockNumber<10; BlockNumber++)
{
/* Move the position indicator to the specified element. */
if ( (fseek(fin, BlockNumber*sizeof(store), SEEK_SET)) !=0)
{
fprintf(stderr, " Error using fseek().");
exit(1);
}
/* Read in a single info. */
fread( info, sizeof(company), 1, fin);

fprintf(fout," Block %d ",BlockNumber); //add header text
printInfo (info, fout);
}
return 0;
}

int printInfo company(info, FILE *fout ) /? I am stuck here at this printInfo...Error message says that I need an initilizer before company, but on the template it reads

/void printInfo(fill in calling arguments -- company and output file pointers)

{
// print all the info values
fprintf (fout,"Name: %s ",info->name );
fprintf (fout,"Street: %s ",info->street );
fprintf (fout,"City: %s ",info->city );
fprintf (fout,"State: %s ",info->state );
fprintf (fout,"Zip: %s ",info->zip );
fprintf (fout,"Phone: %s ",info->phone );
fprintf (fout,"Cell: %s ",info->cell );
fprintf (fout, "Rating: %d ",info->rating );
fprintf (fout,"Contact: %s ",info->contact );
return 0;
}

Explanation / Answer

Hi ,
I have modified the program and it runs now. There were couple of things I did-
1. Made printInfo function void everywhere.
2. removed return 0; from this function .
3. where you were getting an error I changed-
int printInfo company(info, FILE *fout ) to void printInfo (company *info, FILE *fout )

/////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// set string lengths
#define STRSHORT 15
#define STRMED 30
#define STRLONG 80
using namespace std; // created as a C++ CodeBlocks project
typedef struct company
{
char name [STRLONG];
char street [STRMED];
char city [STRMED];
char state [STRMED];
char zip [STRSHORT];
char phone [STRSHORT];
char cell [STRSHORT];
int rating;
char contact [STRLONG];
}company;

void printInfo(company* info, FILE *fp);
int main()
{
FILE *fin;
FILE *fout;
int BlockNumber;
char store[STRLONG]; //create an input file name (think up a name)
char plumbing[STRLONG]; //create an output file name
company* info= new company;
printf("Enter input file name :");
scanf("%s", store );
fflush(stdin); /* flush keyboard buffer */
// open binary data file
if ((fin = fopen("pipes.bin", "rb")) ==NULL)
{
fprintf(stderr, "Error opening input file.");
exit(1);
}
printf("Enter output file name :");
scanf("%s", plumbing);
fflush(stdin); /* flush keyboard buffer */
// open output text file
if ((fout = fopen ("C:/TEMP/plumbing.txt", "w+"))==NULL)
{
fprintf(stderr, "Error opening output file.");
exit(1);
}
for (BlockNumber=0; BlockNumber<10; BlockNumber++)
{
/* Move the position indicator to the specified element. */
if ( (fseek(fin, BlockNumber*sizeof(store), SEEK_SET)) !=0)
{
fprintf(stderr, " Error using fseek().");
exit(1);
}
/* Read in a single info. */
fread( info, sizeof(company), 1, fin);
fprintf(fout," Block %d ",BlockNumber); //add header text
printInfo (info, fout);
}
return 0;
}
void printInfo (company *info, FILE *fout )
// I am stuck here at this printInfo...Error message says that I need an initilizer before company,
//but on the template it reads
//void printInfo(fill in calling arguments -- company and output file pointers)
{
// print all the info values
fprintf (fout,"Name: %s ",info->name );
fprintf (fout,"Street: %s ",info->street );
fprintf (fout,"City: %s ",info->city );
fprintf (fout,"State: %s ",info->state );
fprintf (fout,"Zip: %s ",info->zip );
fprintf (fout,"Phone: %s ",info->phone );
fprintf (fout,"Cell: %s ",info->cell );
fprintf (fout, "Rating: %d ",info->rating );
fprintf (fout,"Contact: %s ",info->contact );
}
//Regards,
//D

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote