Download presentation
Presentation is loading. Please wait.
1
INC 161 , CPE 100 Computer Programming
Lab 9
3
Open your notepad (or other text editor).
Write the following and save. 3,2,5 1,7,13 Rename the extension to .csv Open with excel. Open with notepad.
4
Comma-Separated Values (CSV)
A text file that has its values as numbers or strings, separated by commas and new lines. 3 2 5 1 7 13 Note: This is not excel workbook format.
5
Example 1 (Write file) w = write File handle Open file Write
#include <stdio.h> main() { FILE *fp; fp = fopen(“file.txt”, “w”); fprintf(fp,“Hello”); fclose(fp); } File handle Open file Write Close file
6
Example 1 (Write file) File handle Open file Write Close file
#include <stdio.h> main() { FILE *fp; fp = fopen(“file.csv”, “w”); fprintf(fp,“3,2,5\n”); fprintf(fp,“1,7,13\n”); fclose(fp); } File handle Open file Write Close file
7
Example 2 (Read file) #include <stdio.h> main() { int a,b,c;
FILE *fp; fp = fopen(“file.csv”, “r”); fscanf(fp,“%d,%d,%d”,&a,&b,&c); fclose(fp); }
8
Example 3 (Read file) Check out the return from scanf
#include <stdio.h> main() { int a,b,c,d; FILE *fp; fp = fopen(“file.csv”, “r”); d = fscanf(fp,“%d,%d,%d”,&a,&b,&c); fclose(fp); } Check out the return from scanf
9
Data Science Investigate the Pokemon database. # Name Type 1 Type 2
Total HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary 1 Bulbasaur Grass Poison 318 45 49 65 FALSE 2 Ivysaur 405 60 62 63 80 3 Venusaur 525 82 83 100 VenusaurMega Venusaur 625 123 122 120 4 Charmander Fire 309 39 52 43 50 5 Charmeleon 58 64 6 Charizard Flying 534 78 84 109 85
10
Task 1 Print all the pokemon’s name on the screen. Read header once.
Use loop for each subsequence lines. Instead of using %s for string Use %[^\n,]
11
Task 2 Write a program to print the pokemon’s name that has the highest HP. (for Student ID 01-10) Attack (for Student ID 11-20) Defense (for Student ID 21-30) Total (for Student ID 31-) Hint: Use find minimum in lecture 6 (array)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.