FILE PROCESSING

SO, i'm back guys with a new topic, which is file processing, so on the session we learn HOW to process a file,in addition first i will explain struct, which means you can save various data items with different or the same data types, not like array where we can only use 1 data type, so let me begin, in here, we will first declare the libraries, then we have to write FILE *fp, fp is just the name,so you can make it basically ANYTHING, such as FILE *algorithmandprogramming, but it's better to have meaningful name in order not to confuse yourself, and the file has to use capsclock, what's the pointer here, it just points to us that " Hey, lad, everytime you open a file in this compiler, you need to save it in this fp location that i point right now temporarily" . So, what do i mean by temporary, is that the fact that you have to close up the file after you're done processing it. Firstly, i make void function for add_file and view_data, which is used as the name said, this doesn't have anything to do with file yet, it's just to add data to the struct that has been declared before, you can pass it using value parameter, or use scan it and secondly is to view data after it runs on your compiler. So, the important void function is read_file and write_file, read_file is to read your file and transfer it to your compiler, which is then saved in your struct, so you have to open the file using "r", scan it using fscanf function that is also part of <stdio.h> then close the file, note that the file has to exists beforehand.And lastly, write_file ,the difference is you open your file using "w" which means that the file can be created before existing first, after that you can print data from your compiler to your file using fprintf which is also part of <stdio.h>, and then close your file. This is the sample code.

#include<stdio.h>
#include<string.h>
FILE *fp;

int count=0;
struct weapons
{
char name[100];
int price;
};
void add_file(struct weapons finalfantasy[],char name[],int price);
void view_data(struct weapons finalfantasy[]);
void read_file(struct weapons finalfantasy[]);
void write_file(struct weapons finalfantasy[]);

int main()
{
struct weapons finalfantasy[100];
int ffprice,c;
char fweapon[100];
read_file(finalfantasy);
scanf("%d", &c);getchar();
for(int j=0;j<c;j++)
{
scanf("%s %d", &fweapon, &ffprice);getchar();
add_file(finalfantasy,fweapon,ffprice);
}
add_file(finalfantasy,"super duper powerful greatest weapon",12345);
view_data(finalfantasy);
write_file(finalfantasy);

return 0;
}

void add_file(struct weapons finalfantasy[],char name[],int price)
{
strcpy(finalfantasy[count].name,name);
finalfantasy[count].price=price;
count++;
}

void view_data(struct weapons finalfantasy[])
{
for(int i=0;i<count;i++)
{
printf("%s %d\n", finalfantasy[i].name, finalfantasy[i].price);
}
}

void read_file(struct weapons finalfantasy[])
{
fp=fopen("lol.txt","r");
while(!feof(fp))
{
fscanf(fp,"%s %d\n", &finalfantasy[count].name, &finalfantasy[count].price);
count++;
}
fclose(fp);
}

void write_file(struct weapons finalfantasy[])
{
fp=fopen("test.txt", "a");
for(int i=0;i<count;i++)
{
fprintf(fp,"%s %d\n", finalfantasy[i].name, finalfantasy[i].price);
}
fclose(fp);
}

Luis Indracahya
2201758934
luis.indracahya@binus.ac.id
skyconnectiva.com

Komentar

Postingan populer dari blog ini

Algorithm and programming-review

Cloud Computing