Commit b1f764df by sakomm

Add new file

parents
Showing with 64 additions and 0 deletions
#include <stdio.h>
/*
struct PR2_data {
char names[10];
int value;
char hexnum[10];
};
* */
typedef struct PR2_data {
char names[10];
int value;
char hexnum[10];
} PR2_data;
void func(int);
int main(void){
FILE *task_file;
int i;
PR2_data data[10];
char buf[256];
char buf2[3][256];
int counter = 0;
char file_name[] = "PR2_data.txt";
task_file = fopen(file_name,"r");
if(task_file == NULL){
printf("File not found.");
return -1;
}
//This counts the num of elements in file (30)
while(fscanf(task_file, "%s", buf) != EOF){//reads single element into char
//array buffer
counter++;//increment counter for each element read
}
printf("%d\n", counter);
rewind(task_file);//Reset file pointer to start, to read file again
counter = 0;
//This counts the num of lines in file (10)
while(fscanf(task_file, "%s %s %s", buf2[0], buf2[1], buf2[2]) != EOF){
//reads entire line(need to know line elements)
counter++;//
}
printf("%d\n", counter);
rewind(task_file);//Reset file pointer to start, to read file again
i = 0;
while(fscanf(task_file, "%s %d %s",
data[i].names, &data[i].value, data[i].hexnum) != EOF){
printf("%s %d %s\n", data[i].names, data[i].value, data[i].hexnum);
i++;
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment