EXERCISE Arrays, structs and file processing. Question An online store sells various types of children’s clothing. You are to create a program that stores.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
UNIT 15 File Processing.
We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
1 CSSE 332 Structures, Command Line Arguments. 2 Multi-dimensional arrays Multi-dimensional arrays int points[3][4]; points [1][3] = 12; /* NOT points[3,4]
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
CMPE13 Cyrus Bazeghi Hands on with Functions and IO.
CSE1301 Computer Programming: Revision. Topics Type of questions What do you need to know? About the exam Exam technique Sample questions.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Files Working with Files in C ATS 315. Files Misunderstandings about “files” In Windows and on Macs, we tend to think of files as “containing something”.
How to start Visual Studio 2008 or 2010 (command-line program)
Nested LOOPS.
C Programming – Part 6 File Input and Output
 2007 Pearson Education, Inc. All rights reserved C File Processing.
CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:
1 CSE1301 Computer Programming: Revision. 2 Topics Type of questions What do you need to know? About the exam Exam technique Staff consultation Revision.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
EXERCISE Arrays, structs and file processing. Question You own a pet store. You want to keep an inventory of all the pets that you have. Pets available.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
THE C PROGRAM WORKSHOP. Workshop Features Door for expected results to go out Door for requests to go in Door for unexpected results to go out Bins for.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Computer Programming for Engineers
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
Files A collection of related data treated as a unit. Two types Text
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Lesson #5 Repetition and Loops.
Lesson #5 Repetition and Loops.
University of Washington Computer Programming I
File Access (7.5) CSE 2031 Fall July 2018.
File Input/Output.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Lesson #5 Repetition and Loops.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
File Input and Output.
File Handling.
Hmjngj jxhngh.
Lesson #5 Repetition and Loops.
The while Looping Structure
CSc 352 File I/O Saumya Debray Dept. of Computer Science
ICS103: Programming in C 6: Pointers and Modular Programming
Files Chapter 8.
Presentation transcript:

EXERCISE Arrays, structs and file processing

Question An online store sells various types of children’s clothing. You are to create a program that stores the inventory. Use a struct for each clothing item. Each item will have an ID, type, cost and price. Your program should read the input from keyboard, and then stores it into a file named “stock.dat”. Make sure that the new data is appended to the existing file (Don’t overwrite)

Limit your items to 150 only each time. Your program can also – display a list of particular items. Example, if a user asks for ‘dress’, your program will display a list of all dresses available in the stock – display the total cost of all clothings.

Steps to solve 1)Define the struct struct sItem { int ID; char sType[15]; float cost; float price; };

2)Limit of item is 150. So, use an Array of struct. struct sItem sShop [150] ;

You need to read from users ( = keyboard). Because you have an array of struct (sShop[x]), you can put the inputs there. printf (“Enter ID : “ ); scanf (“%d”, &sShop[x].ID); printf (“Enter type: “); scanf (“%s”, &sShop[x].sType);

Need to write into a file FILE *cPtr; cPtr = fopen (“stock.dat”, “a”); … … fprintf (cPtr, “\n %d %s %.2f %.2f”, sShop[x].ID, sShop[x].sType, sShop[x]. cost, sShop[x]. price) ; //this should be done in a loop fclose (cPtr);

Because it is from user input, you must ask the how many items he/she wants to enter the data int numItems; printf (“How many items you want to enter ? Maximum is 40); scanf (“%d”, &numItems);

An array of items, so must have ‘for’ loops for (x = 0; x < numitems; x++) {.. }

Need to calculate the total cost, so add inside the loop for (x = 0; x < numitems; x++) {.. totalCost + = sShop [ x ].cost; } //then, outside the loop printf (“total Cost: %.2f”, totalCost);

To display the list of a type of clothing, need to ask user the item requested printf (“Enter a type of item to search :”); scanf (“%s”, &searchKey);

need to read from the file to search for items requested by user cPtr = fopen(“stocks.dat”, “r”); while (!feof(cPtr)) { fscanf (cPtr, “%d %s %f”, &ID, &type, &price); if ((strcmp(type, searchKey)==0)) printf (“\n %d %s %f”, ID, type, price); } fclose (cPtr);

Skeleton of a full program #include struct sItem { int ID; char sType[15]; float cost; float price; };

int main() { struct sItem sShop [150] ; FILE *cPtr; cPtr = fopen (“stocks.dat”, “a”); int numitems; float totalCost = 0; printf (“How many items you want to enter ? Maximum is 40); scanf (“%d”, &numitems);

for (x = 0; x < numitems; x++) { printf (“Enter ID : “ ); scanf (“%d”, &sShop[x].ID); printf (“Enter type: “); scanf (“%s”, &sShop[x].sType); printf (“Enter cost: “); scanf (“%s”, &sShop[x].cost); printf (“Enter price: “); scanf (“%s”, &sShop[x].price);

//continue loop block fprintf (cPtr, “\n %d %s %.2f %.2f”, sShop[x].ID, sShop[x].sType, sShop[x]. cost, sShop[x].price); totalCost + = sShop [ x ].cost; } //end of loop 1 fclose (cPtr); printf (“total Cost: %.2f”, totalCost);

printf (“Enter a type of item to search :”); scanf (“%s”, &searchKey); cPtr = fopen(“stocks.dat”, “r”); //open again but for read-only while (!feof(cPtr)) { fscanf (cPtr, “%d %s %f”, &ID, &type, &price); if ((strcmp(type, searchKey)==0)) printf (“\n %d %s %f”, ID, type, price); } fclose (cPtr); return 0; } //end of main

End notes! After you have obtained the skeleton program, expect to find some errors, e.g: – Undeclared variables – Misspellings – Wrongly located code segments However, that is fine. At least you already have a framework where you can work on to refine and rectify.