alexa-tracking

Main Content

1024
1024
KASKUS
51
244
https://www.kaskus.co.id/thread/52c67961a3cb17f2788b4662/operasi-file-bagaimana-cara-delete-file-dengan-c
operasi file, Bagaimana cara delete file dengan C?
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
char *name;
float gpa;
int i;
}student;
int main(int argc, char *argv[])
{
int menu,i;
char ulang;
FILE *pf;
char karakter;
int data[5];
char judul[30],penerbit[40],pengarang[12];

do
{
printf("Menu Utama Data Perpustakaan\n");
printf("--------------------------\n");
printf("1. Tampil Data \n");
printf("2. Tambah Data \n");
printf("3. Edit Data \n");
printf("4. Remove data \n");
printf("5. Search data \n");
printf("--------------------------\n");

printf("Masukan pilihan anda : "); scanf("%d",&menu);
printf("\n----------------------------\n");

switch(menu)
{
case 1:
{
pf = fopen("D:/tugas/file/tugas/data.txt","r");
if (pf != NULL)
{
while ((karakter = getc(pf)) != EOF)
{
printf("%c",karakter);
}
}
else
{
printf("Kesalahan : File coba.txt tidak dapat dibuka");
exit(EXIT_FAILURE);
}
break;
}
case 2:
{
pf = fopen("D:/tugas/file/tugas/data.txt","a+");
//printf("Masukan data berupa bilangan bulat \n");
/*for (i=0; i<5; i++)
{
printf("Data %d : ",i+1); scanf("%d",&data[i]);
}*/
printf("Masukan Judul buku : "); fflush(stdin); gets(judul);
printf("Masukan Penerbit : "); fflush(stdin); gets(penerbit);
printf("Masukan pengarang : "); fflush(stdin); gets(pengarang);

if (pf != NULL)
{
/*for (i=0; i<5; i++)
{
fprintf(pf,"Data ke-%d : %d \n",i+1, data[i]);
}*/
fprintf(pf,"Judul buku : %s \n",judul);
fprintf(pf,"Penerbit : %s \n",penerbit);
fprintf(pf,"pengarang : %s \n",pengarang);
fprintf(pf,"---------------------------\n");
}
case 3:
{
int main()
{
FILE *originalFile = fopen("D:/tugas/file/tugas/data.txt","r");
FILE *newFile = fopen("D:/tugas/file/tugas/data_temp.txt","w");
int dataCount;
fscanf(originalFile, "%i", &dataCount);

data *passingdatas = malloc(sizeof(data) * dataCount);

int passingCount = 0;

for i = 0 to i < dataCount,i++;
{
int nameLen;
fscanf(originalFile, "%i", &nameLen);
char *name = malloc(sizeof(char) * (nameLen + 1));
name[nameLen] = '\0';
float gpa;
fscanf(originalFile, "%s %f", name, &gpa);
if (gpa >=65.0)
{
passingdatas[passingCount].name = name;
passingdatas[passingCount].gpa = gpa;

passingCount++;
}
}
fprintf(newFile, "%i\n",passingCount);

for (int i = 0; i < passingCount; i++)
fprintf(newFile, "%i %s %.2f\n", strlen(passingdatas[i].name, passingdatas[i].gpa));
fclose(originalFile);
fclose(newFile);
remove("D:/tugas/file/tugas/data.txt");
rename("D:/tugas/file/tugas/data_temp.txt" , "D:/tugas/file/tugas/data.txt");
}
}

//if (pf != NULL)
// {
/*for (i=0; i<5; i++)
{
fprintf(pf,"Data ke-%d : %d \n",i+1, data[i]);
}*/
// fprintf(pf,"Judul buku : %s \n",judul);
// fprintf(pf,"Penerbit : %s \n",penerbit);
// fprintf(pf,"pengarang : %s \n",pengarang);

// fprintf(pf,"---------------------------\n");*/

else
{
printf("Kesalahan : File coba.txt tidak dapat dibuka");
exit(EXIT_FAILURE);
}
fclose(pf);
break;
}


}
printf("\nProses di ulang [y/t]: "); fflush(stdin); scanf("%c",&ulang);
system("cls");
}
while (ulang != 't') ;


//system("PAUSE");
return 0;
}



Untuk tambah data sama lihat data bisa gan tapi untuk delete search sama edit gak bisa2 gan

help ya gan


eror compile nya

Code:
C:\Users\jurs\Desktop\tugas.c||In function `main':|
C:\Users\jurs\Desktop\tugas.c|22|error: 'for' loop initial declaration used outside C99 mode|
C:\Users\jurs\Desktop\tugas.c|26|error: `nameLen' undeclared (first use in this function)|
C:\Users\jurs\Desktop\tugas.c|26|error: (Each undeclared identifier is reported only once|
C:\Users\jurs\Desktop\tugas.c|26|error: for each function it appears in.)|
C:\Users\jurs\Desktop\tugas.c|45|error: redefinition of 'i'|
C:\Users\jurs\Desktop\tugas.c|22|error: previous definition of 'i' was here|
C:\Users\jurs\Desktop\tugas.c|45|error: 'for' loop initial declaration used outside C99 mode|
C:\Users\jurs\Desktop\tugas.c|46|error: `pasiingStudents' undeclared (first use in this function)|
C:\Users\jurs\Desktop\tugas.c|46|error: too many arguments to function `strlen'|
C:\Users\jurs\Desktop\tugas.c|57|error: syntax error before "return"|
||=== Build finished: 10 errors, 0 warnings (0 minutes, 0 seconds) ===|