How to write reporting function on this question This program implements the buying and selling of stocks. It starts by printing a welcome message Welcome to mystocks.com Then a main menu of choices for the user is output. Reporting, buying or selling? (0=quit, 1=report, 2=buy, 3=sell): The program ends with a goodbye message. Thank you for trading with mystocks.com Use doubly linked list of stock_t structures. stock_t structure looks like: #define MAX_TICKER_LENGTH 6 typedef struct stock_t { char ticker[MAX_TICKER_LENGTH]; date_t date; // date bought int numShares; double pricePerShare; } stock_t; date_t structure looks like: typedef struct date_t { int month, day, year; } date_t; Create the following files in a directory: date.h: contains the above date_t structure • stock.h and stock.c: contain the stock structure and any constants and stock function prototypes. Eg: a print stock function. • node.h, node.c: contain at least the node typdef and initNode function • list.h and list.c: contain the doubly linked list structure and any constants and list function prototypes. Start with the pair programming list files and add function(s) for selling (eg: searching the list). All functions that operate on the list as a whole should go here. • main.c contains the main function and, possibly, other functions such as functions to report, buy and sell. • Makefile: a makefile to make each source file separately or the entire program using the gcc compiler (not g++) and to “clean” the directory (remove all object files and the executable) Reporting: Prints all stocks owned to standard output. After printing stocks owned, ask the user which stock to report on and print all of the details of that stock. Stocks owned are stored in binary files with the stock ticker symbol as the name in all capital letters. For example, Microsoft stocks would be in a file called MSFT.bin and Google stocks in a file called GOOG.bin. In order to report, the program should use the following types and functions from dirent.h (“directory entry” header file). A single directory entry has information in it such as file name. o DIR* dirPtr: a pointer to a DIR, a directory. Used by the opendir function to open a directory for reading. This is analogous to a FILE* (file pointer) used for reading/writing from/to files that are not directories. o struct dirent* dirEntry: a pointer to a directory entry structure. The readdir function returns one of these, a directory entry, which can be used to get the name of the file that the entry pertains to, dirEntry->d_name o DIR* opendir( char* ): takes the path of a directory such as “.” for the current directory and opens it. It returns a DIR* or NULL if the open was unsuccessful (i.e., the directory doesn’t exist). The opendir function is analogous to the fopen function used for opening files. dirPtr =opendir( “.” ); o struct dirent* readdir( DIR* ): takes a DIR* (a directory) and returns a structure representing the next entry in the directory. The function returns NULL if there are no more entries in the directory. while( (dirEntry = readdir( dirPtr )) != NULL ) { // use dirEntry->d_name and see if it’s a *.bin” // file. If it is, make a string out of the name // only (without the “.bin”), and print it } o int closedir( DIR* ): closes a directory pointer just like you would close a file after reading from it. closedir( dirPtr ); Buying: Prompts the user for the ticker symbol, the number of shares to buy and the price per share. It opens a binary file for that ticker symbol in append mode: FILE* output; output = fopen( filename, "a" ); gets the date from the system, fills up a stock_t structure with this information and writes the structure to the binary file. Closes the binary file. Selling: Prompts the user for ticker symbol of stock. Opens that stock’s binary file for reading. If the file doesn’t exist, prints a message indicating that the user doesn’t own any of that stock. Otherwise, the program reads all of the stocks from the file and puts them in a queue then closes the file. The program prints out how many shares of that stock the user has and asks the user for the number of stocks to sell and the current stock price. If the user doesn’t own that many shares total, output an error message and reprint the main menu above (report, buy, sell or quit). Otherwise, go through the queue of stocks removing the number of shares needed and calculating and printing the total price to buy the stocks, the total selling price, and the gains (or losses). Open the file again in write mode and writes the entire file from the updated contents of the list. Close the file. If the user sells all shares of a stock in a file, then the program has to delete the file with the remove function in stdio.h which takes a filename (char *) as a parameter. remove(
How to write reporting function on this question This program implements the buying and selling of stocks. It starts by printing a welcome message Welcome to mystocks.com Then a main menu of choices for the user is output. Reporting, buying or selling? (0=quit, 1=report, 2=buy, 3=sell): The program ends with a goodbye message. Thank you for trading with mystocks.com Use doubly linked list of stock_t structures. stock_t structure looks like: #define MAX_TICKER_LENGTH 6 typedef struct stock_t { char ticker[MAX_TICKER_LENGTH]; date_t date; // date bought int numShares; double pricePerShare; } stock_t; date_t structure looks like: typedef struct date_t { int month, day, year; } date_t; Create the following files in a directory: date.h: contains the above date_t structure • stock.h and stock.c: contain the stock structure and any constants and stock function prototypes. Eg: a print stock function. • node.h, node.c: contain at least the node typdef and initNode function • list.h and list.c: contain the doubly linked list structure and any constants and list function prototypes. Start with the pair programming list files and add function(s) for selling (eg: searching the list). All functions that operate on the list as a whole should go here. • main.c contains the main function and, possibly, other functions such as functions to report, buy and sell. • Makefile: a makefile to make each source file separately or the entire program using the gcc compiler (not g++) and to “clean” the directory (remove all object files and the executable) Reporting: Prints all stocks owned to standard output. After printing stocks owned, ask the user which stock to report on and print all of the details of that stock. Stocks owned are stored in binary files with the stock ticker symbol as the name in all capital letters. For example, Microsoft stocks would be in a file called MSFT.bin and Google stocks in a file called GOOG.bin. In order to report, the program should use the following types and functions from dirent.h (“directory entry” header file). A single directory entry has information in it such as file name. o DIR* dirPtr: a pointer to a DIR, a directory. Used by the opendir function to open a directory for reading. This is analogous to a FILE* (file pointer) used for reading/writing from/to files that are not directories. o struct dirent* dirEntry: a pointer to a directory entry structure. The readdir function returns one of these, a directory entry, which can be used to get the name of the file that the entry pertains to, dirEntry->d_name o DIR* opendir( char* ): takes the path of a directory such as “.” for the current directory and opens it. It returns a DIR* or NULL if the open was unsuccessful (i.e., the directory doesn’t exist). The opendir function is analogous to the fopen function used for opening files. dirPtr =opendir( “.” ); o struct dirent* readdir( DIR* ): takes a DIR* (a directory) and returns a structure representing the next entry in the directory. The function returns NULL if there are no more entries in the directory. while( (dirEntry = readdir( dirPtr )) != NULL ) { // use dirEntry->d_name and see if it’s a *.bin” // file. If it is, make a string out of the name // only (without the “.bin”), and print it } o int closedir( DIR* ): closes a directory pointer just like you would close a file after reading from it. closedir( dirPtr ); Buying: Prompts the user for the ticker symbol, the number of shares to buy and the price per share. It opens a binary file for that ticker symbol in append mode: FILE* output; output = fopen( filename, "a" ); gets the date from the system, fills up a stock_t structure with this information and writes the structure to the binary file. Closes the binary file. Selling: Prompts the user for ticker symbol of stock. Opens that stock’s binary file for reading. If the file doesn’t exist, prints a message indicating that the user doesn’t own any of that stock. Otherwise, the program reads all of the stocks from the file and puts them in a queue then closes the file. The program prints out how many shares of that stock the user has and asks the user for the number of stocks to sell and the current stock price. If the user doesn’t own that many shares total, output an error message and reprint the main menu above (report, buy, sell or quit). Otherwise, go through the queue of stocks removing the number of shares needed and calculating and printing the total price to buy the stocks, the total selling price, and the gains (or losses). Open the file again in write mode and writes the entire file from the updated contents of the list. Close the file. If the user sells all shares of a stock in a file, then the program has to delete the file with the remove function in stdio.h which takes a filename (char *) as a parameter. remove(
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
How to write reporting function on this question
This program implements the buying and selling of stocks. It starts by printing a welcome message
Welcome to mystocks.com
Then a main menu of choices for the user is output.
Reporting, buying or selling?
(0=quit, 1=report, 2=buy, 3=sell):
The program ends with a goodbye message.
Thank you for trading with
mystocks.com
Use doubly linked list of stock_t structures. stock_t structure looks like:
#define MAX_TICKER_LENGTH 6
typedef struct stock_t {
char ticker[MAX_TICKER_LENGTH];
date_t date; // date bought
int numShares;
double pricePerShare;
} stock_t;
date_t structure looks like:
typedef struct date_t {
int month, day, year;
} date_t;
Create the following files in a directory:
date.h: contains the above date_t structure
• stock.h and stock.c: contain the stock structure and any constants and stock function prototypes. Eg: a print stock function.
• node.h, node.c: contain at least the node typdef and initNode function
• list.h and list.c: contain the doubly linked list structure and any constants and list function prototypes. Start with the pair programming list files and add function(s) for selling (eg: searching the list). All functions that operate on the list as a whole should go here.
• main.c contains the main function and, possibly, other functions such as functions to report, buy and sell.
• Makefile: a makefile to make each source file separately or the entire program using the gcc compiler (not g++) and to “clean” the directory (remove all object files and the executable)
Reporting: Prints all stocks owned to standard output. After printing stocks owned, ask the user which stock to report on and print all of the details of that stock. Stocks owned are stored in binary files with the stock ticker symbol as the name in all capital letters. For example, Microsoft stocks would be in a file called MSFT.bin and Google stocks in a file called GOOG.bin. In order to report, the program should use the following types and functions from dirent.h (“directory entry” header file). A single directory entry has information in it such as file name.
o DIR* dirPtr: a pointer to a DIR, a directory. Used by the opendir function to open a directory for reading. This is analogous to a FILE* (file pointer) used for reading/writing from/to files that are not directories.
o struct dirent* dirEntry: a pointer to a directory entry structure. The readdir function returns one of these, a directory entry, which can be used to get the name of the file that the entry pertains to, dirEntry->d_name
o DIR* opendir( char* ): takes the path of a directory such as “.” for the current directory and opens it. It returns a DIR* or NULL if the open was unsuccessful (i.e., the directory doesn’t exist). The opendir function is analogous to the fopen function used for opening files.
dirPtr =opendir( “.” );
o struct dirent* readdir( DIR* ): takes a DIR* (a directory) and returns a structure representing the next entry in the directory. The function returns NULL if there are no more entries in the directory.
while( (dirEntry = readdir( dirPtr )) != NULL ) {
// use dirEntry->d_name and see if it’s a *.bin”
// file. If it is, make a string out of the name
// only (without the “.bin”), and print it
}
o int closedir( DIR* ): closes a directory pointer just like you would close a file after reading from it.
closedir( dirPtr );
Buying: Prompts the user for the ticker symbol, the number of shares to buy and the price per share. It opens a binary file for that ticker symbol in append mode:
FILE* output;
output = fopen( filename, "a" );
gets the date from the system, fills up a stock_t structure with this information and writes the structure to the binary file. Closes the binary file.
Selling: Prompts the user for ticker symbol of stock. Opens that stock’s binary file for reading. If the file doesn’t exist, prints a message indicating that the user doesn’t own any of that stock. Otherwise, the program reads all of the stocks from the file and puts them in a queue then closes the file. The program prints out how many shares of that stock the user has and asks the user for the number of stocks to sell and the current stock price. If the user doesn’t own that many shares total, output an error message and reprint the main menu above (report, buy, sell or quit). Otherwise, go through the queue of stocks removing the number of shares needed and calculating and printing the total price to buy the stocks, the total selling price, and the gains (or losses). Open the file again in write mode and writes the entire file from the updated contents of the list. Close the file. If the user sells all shares of a stock in a file, then the program has to delete the file with the remove function in stdio.h which takes a filename (char *) as a parameter.
remove( filename );
Expert Solution
Step 1
To write the reporting function, you can follow these steps:
1.Open the current directory using the opendir
function from dirent.h
.
DIR* dirPtr = opendir(".");
2. Check if the directory was opened successfully, and print an error message and return if not.
if (dirPtr == NULL) {
printf("Error opening directory\n");
return;
}
printf("Error opening directory\n");
return;
}
3.Loop through the directory entries using the
readdir
function, and check if each entry is a .bin
file by checking the extension using the strstr
function from string.h
.struct dirent* dirEntry;
while ((dirEntry = readdir(dirPtr)) != NULL) {
char* filename = dirEntry->d_name;
if (strstr(filename, ".bin") != NULL) {
// This is a .bin file, print its name
printf("%s\n", filename);
}
}
while ((dirEntry = readdir(dirPtr)) != NULL) {
char* filename = dirEntry->d_name;
if (strstr(filename, ".bin") != NULL) {
// This is a .bin file, print its name
printf("%s\n", filename);
}
}
4.Close the directory using the
closedir
function.
closedir(dirPtr);
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education