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

Hello, I having trouble getting a simple program involving data structures, stri

ID: 3619866 • Letter: H

Question

Hello, I having trouble getting a simple program involving data structures, strings and file I/O to work. I need someone to help me get the operation to work correctly.

The program reads from a text file (.txt) and uses the read data to assign it to the fields of a struct. The data structure has 3 fields: an artist name (C string), an album name (C String), and a year of release.
Here is the following code: (The code that has been left out, like the includes and function main is all standard so I omitted it.)
 typedef struct
{
char* name;
char* nalbum;
int year;
} ALBUM;
 FILE* strm = fopen("cds.txt","r");
char arr1[SIZE];
ALBUM dummy = { "test", "test", 2010};
ALBUM* dataPtr;

while ( fgets (arr1, sizeof(arr1), strm) != NULL)
{
/* An album artist was read, get the rest of the information */
strcat (dummy.name, arr1);
printf ("%s ",dummy.name);
if (fgets (arr1, sizeof (arr1), strm) != NULL)
{
/* This album has a title */
strcat(dummy.nalbum, arr1);
printf ("%s ",dummy.nalbum);
}
else
{
/* Incomplete Artist and Album information! */
dummy.nalbum = "Unknown";
printf ("%s ",dummy.nalbum);

}
if (fgets (arr1, sizeof (arr1), strm) != NULL)
{
/* A year was given, data collection complete! */
dummy.year = atoi (arr1);
printf("%d ", dummy.year);
}
else
{
/* Incomplete Artist and Album Information ! */
dummy.year = 2010;
printf("%d ", dummy.year);
}
}

printf ("%s %s %d ", dummy.name, dummy.nalbum, dummy.year);


==============================================================
The text file (cds.txt) looks like this:
Artist Name
Album Name
<Year of Release>

So the data structure would look like this when the text file is read
correctly:
ALBUM Something = {"Artist Name", "Album Name", 2010}


The problem is with my algorithm, using the code below, when I launch the program the fields of dummy are all the same. How can I read the data correctly and have the string pointing to the correct strings
For example: if cds.txt is The Beatles White Album 1968
my program would set to ALBUM dummy as {"1968", "1968", 1968}. I suppose the
strcat () function is not working properly either.


The program reads from a text file (.txt) and uses the read data to assign it to the fields of a struct. The data structure has 3 fields: an artist name (C string), an album name (C String), and a year of release.
Here is the following code: (The code that has been left out, like the includes and function main is all standard so I omitted it.)
 typedef struct
{
char* name;
char* nalbum;
int year;
} ALBUM;
 typedef struct
{
char* name;
char* nalbum;
int year;
} ALBUM;
 FILE* strm = fopen("cds.txt","r");
char arr1[SIZE];
ALBUM dummy = { "test", "test", 2010};
ALBUM* dataPtr;

while ( fgets (arr1, sizeof(arr1), strm) != NULL)
{
/* An album artist was read, get the rest of the information */
strcat (dummy.name, arr1);
printf ("%s ",dummy.name);
if (fgets (arr1, sizeof (arr1), strm) != NULL)
{
/* This album has a title */
strcat(dummy.nalbum, arr1);
printf ("%s ",dummy.nalbum);
}
else
{
/* Incomplete Artist and Album information! */
dummy.nalbum = "Unknown";
printf ("%s ",dummy.nalbum);

}
if (fgets (arr1, sizeof (arr1), strm) != NULL)
{
/* A year was given, data collection complete! */
dummy.year = atoi (arr1);
printf("%d ", dummy.year);
}
else
{
/* Incomplete Artist and Album Information ! */
dummy.year = 2010;
printf("%d ", dummy.year);
}
}

printf ("%s %s %d ", dummy.name, dummy.nalbum, dummy.year);


==============================================================
The text file (cds.txt) looks like this:
Artist Name
Album Name
<Year of Release>

So the data structure would look like this when the text file is read
correctly:
ALBUM Something = {"Artist Name", "Album Name", 2010}


The problem is with my algorithm, using the code below, when I launch the program the fields of dummy are all the same. How can I read the data correctly and have the string pointing to the correct strings
For example: if cds.txt is The Beatles White Album 1968
my program would set to ALBUM dummy as {"1968", "1968", 1968}. I suppose the
strcat () function is not working properly either.

 FILE* strm = fopen("cds.txt","r");
char arr1[SIZE];
ALBUM dummy = { "test", "test", 2010};
ALBUM* dataPtr;

while ( fgets (arr1, sizeof(arr1), strm) != NULL)
{
/* An album artist was read, get the rest of the information */
strcat (dummy.name, arr1);
printf ("%s ",dummy.name);
if (fgets (arr1, sizeof (arr1), strm) != NULL)
{
/* This album has a title */
strcat(dummy.nalbum, arr1);
printf ("%s ",dummy.nalbum);
}
else
{
/* Incomplete Artist and Album information! */
dummy.nalbum = "Unknown";
printf ("%s ",dummy.nalbum);

}
if (fgets (arr1, sizeof (arr1), strm) != NULL)
{
/* A year was given, data collection complete! */
dummy.year = atoi (arr1);
printf("%d ", dummy.year);
}
else
{
/* Incomplete Artist and Album Information ! */
dummy.year = 2010;
printf("%d ", dummy.year);
}
}

printf ("%s %s %d ", dummy.name, dummy.nalbum, dummy.year);


==============================================================
The text file (cds.txt) looks like this:
Artist Name
Album Name
<Year of Release>

So the data structure would look like this when the text file is read
correctly:
ALBUM Something = {"Artist Name", "Album Name", 2010}


The problem is with my algorithm, using the code below, when I launch the program the fields of dummy are all the same. How can I read the data correctly and have the string pointing to the correct strings
For example: if cds.txt is The Beatles White Album 1968
my program would set to ALBUM dummy as {"1968", "1968", 1968}. I suppose the
strcat () function is not working properly either.

The problem is with my algorithm, using the code below, when I launch the program the fields of dummy are all the same. How can I read the data correctly and have the string pointing to the correct strings
For example: if cds.txt is The Beatles White Album 1968
my program would set to ALBUM dummy as {"1968", "1968", 1968}. I suppose the
strcat () function is not working properly either.

Explanation / Answer

//here is running program, you take char * in structure , you should take array. #include #include typedef struct { char name[100]; char nalbum[100]; int year; } ALBUM; #define SIZE 100 int main() { FILE* strm = fopen("cds.txt","r"); char arr1[SIZE]; ALBUM dummy= { "test", "test", 2010}; ALBUM* dataPtr; while ( fgets (arr1, sizeof(arr1), strm) != NULL) { /* An album artist was read, get the rest of the information */ strcat (dummy.name, arr1); printf ("->%s ",dummy.name); if (fgets (arr1, sizeof (arr1), strm) != NULL) { /* This album has a title */ strcat(dummy.nalbum, arr1); printf ("%s ",dummy.nalbum); } else { /* Incomplete Artist and Album information! */ strcpy(dummy.nalbum , "Unknown"); printf ("%s ",dummy.nalbum); } if (fgets (arr1, sizeof (arr1), strm) != NULL) { /* A year was given, data collection complete! */ dummy.year = atoi (arr1); printf("%d ", dummy.year); } else { /* Incomplete Artist and Album Information ! */ dummy.year = 2010; printf("%d ", dummy.year); } } printf (" ---------------- %s %s %d ", dummy.name, dummy.nalbum, dummy.year); }
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