Last active
December 23, 2015 08:59
-
-
Save whyrusleeping/6611776 to your computer and use it in GitHub Desktop.
CS 121 Lab 4 Help sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************** | |
Hello Everyone, This is going to be a | |
(hopefully) helpful sheet of notes for lab 4. | |
********************************************/ | |
/* Open A File for reading, and check to make sure it exists */ | |
FILE *some_file = NULL; | |
some_file = fopen("my_file_name.txt", "r"); | |
if (some_file == NULL) { | |
printf("This file does not exist!!\n"); | |
exit(1); //Cause program to die (you can do something else here too_ | |
} | |
//File is now open. | |
/* Read diferent things from an already opened file */ | |
int num_read = 0; | |
int my_number = 0; | |
double my_double = 0; | |
char my_letter = 0; | |
//fscanf returns the number of items it reads | |
//this might be useful to check! | |
num_read = fscanf(my_file, "%d%lf", &my_number, &my_double); | |
if (num_read != 2) { | |
printf("Error: couldnt read correctly from file!\n"); | |
exit(1); | |
} | |
num_read = fscanf(my_file, "%c", &my_letter); | |
if (num_read != 1) { | |
printf("Error: couldnt read a char from the file!\n"); | |
exit(1); | |
} | |
/**************************** | |
* Switch Statements! * | |
****************************/ | |
switch (anumber) { | |
case 1: | |
printf("Its a one!\n"); | |
break; | |
case 2: | |
printf("Its two ones! (or a two)\n"); | |
break; | |
default: | |
printf("everything else is boring...\n"); | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment