Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:00:27+00:00 2026-06-12T05:00:27+00:00

I am slowly learning C, but not very well. I have been reading over

  • 0

I am slowly learning C, but not very well. I have been reading over the countless topics and questions on reading and writing, but I have yet to be able to find anything that makes this all click for me.

I was given the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100

struct YouTubeVideo { 
char video_name[1024];      // YouTube video name
int ranking;                // Number of viewer hits
char url[1024];             // YouTube URL
};

struct YouTubeVideo Collection[MAX];

int tail = 0;

//-- Forward Declaration --// 
void printall();
void insertion();
void branching(char option);
void menu(); 


int main()
{
char ch; 

// TODO: Add code to load save data from file

printf("\n\nWelcome to CSE240: YouTube Classic Hits\n");

do {
     menu();
     fflush(stdin);           // Flush the standard input buffer 
     ch = tolower(getchar()); // read a char, convert to lower case
     branching(ch);
} while (ch != 'q');

return 0; 
}

void menu()
{
printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
printf("i: Insert a new favorite\n");
printf("p: Review your list\n"); 
printf("q: Save and quit\n");
printf("\n\nPlease enter a choice (i, p, or q) ---> "); 
}

void branching(char option)
{
switch(option)
{
    case 'i':
        insertion();
    break;

    case 'p':
        printall();
    break;

    case 'q':
        // TODO: Add code to save data into a file
    break;

    default:
        printf("\nError: Invalid Input.  Please try again..."); 
    break;
}
}

void insertion()
{
if(tail < MAX)
{
    printf("\nWhat is the name of the video? (No spaces characters allowed)\n");
    scanf("%s", Collection[tail].video_name);

    printf("\nHow many viewer hits does this video have?\n");
    scanf("%d", &Collection[tail].ranking);

    printf("\nPlease enter the URL: ");
    scanf("%s", &Collection[tail].url);

    tail++;
}
else
{
    printf("\nERROR: Your collection is full. Cannot add new entries.\n");
}
}

void printall()
{
int i; 

printf("\nCollections: \n"); 

for(i = 0; i < tail; i++)
{
    printf("\nVideo Name: %s", Collection[i].video_name);
    printf("\nRanking (Hits): %d", Collection[i].ranking);
    printf("\nURL: %s", Collection[i].url);
    printf("\n");
}
}

I am suppose to write the code that will store the collection into a file and the likewise right the code that will load the file and read from it.

Thanks to a fairly helpful TA I was able to formulate the following code for each

void store()
{
FILE * fileName;
fileName = fopen ( "Ranking.dbm" , "wb" );
fwrite ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);

fclose (fileName);
                    }

and

void read()
{
FILE *fileName;
fileName = fopen("ranking.dbm", "rb");
if (fileName != NULL){
    fread ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
}
else {
    printf("ERROR");
                        }   

                    }

I believe these each to function but the real problem is I don’t think I quite understand how and I believe that since I dont even know how they function, I dont know how to use them in the code.

I added both methods to the given code and came up with this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX 100

struct YouTubeVideo {
char video_name[1024];      // YouTube video name
int ranking;                // Number of viewer hits
char url[1024];             // YouTube URL
};

struct YouTubeVideo Collection[MAX];

int tail = 0;

//-- Forward Declaration --//
void printall();
void insertion();
void branching(char option);
void menu();
void store();
void read();


int main()
{
char ch;

read();

printf("\n\nWelcome to CSE240: YouTube Classic Hits\n");

do {
    menu();
    fpurge(stdin);            // Flush the standard input buffer
    ch = tolower(getchar()); // read a char, convert to lower case
    branching(ch);
} while (ch != 'q');

return 0;
}

void menu()
{
printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
printf("i: Insert a new favorite\n");
printf("p: Review your list\n");
printf("q: Save and quit\n");
printf("\n\nPlease enter a choice (i, p, or q) ---> ");
}

void branching(char option)
{

switch(option)
{
    case 'i':
        insertion();
        break;

    case 'p':
        printall();
        break;

    case 'q':
        store();
        break;


    default:
        printf("\nError: Invalid Input.  Please try again...");
        break;
}
}

void insertion()
{
if(tail < MAX)
{
    printf("\nWhat is the name of the video? (No spaces characters allowed)\n");
    scanf("%s", Collection[tail].video_name);

    printf("\nHow many viewer hits does this video have?\n");
    scanf("%d", &Collection[tail].ranking);

    printf("\nPlease enter the URL: ");
    scanf("%s", &Collection[tail].url);

    tail++;
}
else
{
    printf("\nERROR: Your collection is full. Cannot add new entries.\n");
}
}

void printall()
{
int i;

printf("\nCollections: \n");

for(i = 0; i < tail; i++)
{
    printf("\nVideo Name: %s", Collection[i].video_name);
    printf("\nRanking (Hits): %d", Collection[i].ranking);
    printf("\nURL: %s", Collection[i].url);
    printf("\n");
}

}

void store()
{
FILE * fileName;
fileName = fopen ( "Ranking.dbm" , "wb" );
if (fileName != NULL)
    {
        fwrite ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
        fclose (fileName);
    }

else {
    perror("Following error occurred(): ");
}
                    }



void read()
{
FILE *fileName;
fileName = fopen("Ranking.dbm", "rb");
if (fileName != NULL)
    {
        fread ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
        fclose(fileName);
    }

else {
    perror("Following error occurred with fopen(): ");
                        }   

                    }

Now I am sure anyone who has read this has probably already face palmed themselves cause they see the problem, but I do not. The code does not create the file to write to and likewise it has nothing to read from so I cant even begin to see what is wrong with that.
Now I am not looking for a given answer, but I would really like to know what it is I am doing incorrectly, what concepts I appear to not understand, and how I can go about fixing these. I have done a few hours of research on this already and I realize it is elementary, but I really would like a hand in learning. Its frustrating spending hours on a topic that the professor said should only take a couple of hours to complete at most.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:00:29+00:00Added an answer on June 12, 2026 at 5:00 am

    Minor: Make sure to check the return values for things like fopen, fread, fwrite etc. more often.

    Mild: You’ve got a potential typo in the filename (some operating systems have case-sensitive file names)

    Severe: read() doesn’t set up a value for tail… 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning (slowly but surely) how to do deal with sqlite databases
I'm a complete noob to JQUERY, learning things slowly. But I have been downloaded
Been slowly picking up the basics in IOS Programming, but seemed to have hit
I have been slowly learning iPhone development and seem to keep hitting walls where
I am new to PHP but learning slowly. Problem I have is including Google
A lisp question here. I've been slowly learning lisp over the last couple of
I have been developing a registration form and slowly but surely I have been
I have been slowly learning SQL the last few weeks. I've picked up all
I'm slowly learning the ins and outs of LINQtoSQL, but this is confusing me.
I've been slowly and a bit painfully working my way up the datastore/JDO learning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.