Új hozzászólás Aktív témák
-
Dirty_Pio
csendes tag
Sziasztok, akarok irni egy nagyjabol katalogizalo programot ami eloszor megnyit egy data.txt fielot amiben el vannak mentve a katalogus elemei es a kodjai. A felhasznalo ezutan beirja a kodjat a kivant reszlegnek ami utan a program megnyitja a filet amibe az elemek vannak, es ki is iratja a kepernyore. Ezutan rakerdez hogy akarok-e hozzaadni uj elemet vagy meglevot kitorolni.Minden reszleg, azaz file elso soraba el van mentve hogy hany elem van benne. A problemam az lenne hogy amikor ki akarok torolni egy elemet akkor azt ugy oldalam meg hogy szepen az alatta levo sortol kezdodoen mindent ramasolnak es az utolso sort pedig elhagynam. Itt a kod
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
struct tm * timeinfo;
int rm(FILE *f)
{
fseek(f,0,0);
char buff[255];
int max,linelen,n;
fgets(buff,100,f);
max=atoi(buff);
printf("type the nr of the product");
scanf("%i",&n);
printf("the number %i, and max %i",n,max);
while (n>0)
{
fgets(buff,250,f);
n--;
}
fgets(buff,250,f);
while (fgets(buff,250,f))
{
linelen=strlen(buff);
fseek(f,-linelen,1);
fprintf(f,"%s",buff);
};
return 0;
}
int add(FILE *f)
{
char name[20],price[15],buff[101]="",*s;
int linelen,max;
fseek(f,0,0);
fgets(buff,19,f);
max=atoi(buff);
max++;
// printf("max=%i",max);
fseek(f,0,0);
fprintf(f,"%i\n",max);
fseek(f,0,2);
time_t rawtime;
time(&rawtime);
timeinfo=localtime(&rawtime);
s=asctime(timeinfo);
s[strlen(s)-1]='\0';
fprintf ( f,"\n %i %s ",max,s);
fseek(f,1,1);
gets(buff);
printf("product name:");
scanf("%s",name);
fprintf(f," %s ",name);
printf("product prince:");
gets(buff);
scanf("%s",price);
fprintf(f," %s",price);
return 0;
}
int list(FILE *f)
{
fseek(f,0,0);
char c;
fgetc(f);
while( (c=fgetc(f))!=EOF)
{
putchar(c);
}
printf("\n");
return 0;
}
int main(void)
{
FILE *f,*g;
if (!(g=fopen("data.txt","r"))) {
printf("missing data file");
exit (1);
}
char s[3],c;
int a=1;
list(g);
fclose (g);
while (a)
{
printf("\nchoose the code of the product type\n");
scanf("%s",s);
if (strlen(s)==2)
switch (s[strlen(s)-1])
{
case '1' :
if (!(f=fopen("a1.txt","r+")))
{
printf("missing data file");
exit (1);
}
a=!a;
break;
case '2' :
if (!(f=fopen("a2.txt","r+")))
{
printf("missing data file");
exit (1);
}
a=!a;
break;
case '3' :
if (!(f=fopen("a3.txt","r+")))
{
printf("missing data file");
exit (1);
}
a=!a;
break;
case '4' :
if (!(f=fopen("a4.txt","r+")))
{
printf("missing data file");
exit (1);
}
a=!a;
break;
case '5' :
if (!(f=fopen("a5.txt","r+")))
{
printf("missing data file");
exit (1);
}
a=!a;
break;
case '6' :
if (!(f=fopen("a6.txt","r+")))
{
printf("missing data file");
exit (1);
}
a=!a;
break;
case '7' :
if (!(f=fopen("a7.txt","r+")))
{
printf("missing data file");
exit (1);
}
a=!a;
break;
default :
printf("wrong code\n");
}
}
list(f);
a=1;
while (a)
{
printf("press 1 to add an element, 2 to remove one and 0 to exit");
scanf("%i",&a);
switch(a)
{
case 1:
add(f);
break;
case 2:
rm(f);
break;
default :break;
}
}
fclose (f);
return 0;
}Az rm fuggveny oldana meg a torlest, de ehelyett vegtelen ciklusba kot ki es egyre csak noveli a file meretet. Egy peldat is mellekelek:
file data.txt:
code name
a1 = PC/notebook/server
a2 = cash register/balance
a3 = software
a4 = Husqvarna
a5 = audio systems
a6 = home/car security
a7 = PC/camera accesorriesfile a3.txt
4
nr. __date__ __product_name__ ___price___
1 Mon Oct 05 17:13:14 2009 elem1 1234
2 Mon Oct 05 17:13:22 2009 elem2 3214
3 Mon Oct 05 17:13:28 2009 elem3 2114
4 Mon Oct 05 17:13:33 2009 elem4 2145Es ha tudtok jol fogna egy modszer amivel szepen kozepre tudom amjd igazitani a fielba, mert eddig nekem csak az jutott az eszembe hogy mindig fix szamu karakter keruljon minden nev ala. ELore is kosz.
Új hozzászólás Aktív témák
● olvasd el a téma összefoglalót!
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- iPad 10 Eladó Újszerű GARIS!! INGYEN CSOMAGKÜLDÉS!!
- ASUS TUF A15 - 15.6"FHD 144Hz - Ryzen 7 7445HS - 16GB - 512GB - Win11 - RTX 3050 - Garancia
- Innosick, Samsung Laptop memória DDR5 5600Mhz // ÚJ // Számla // Garancia //
- Új Gamer PC - Ryzen 7 5700X / RX 9060 XT 16GB / B550M WIFI / 32GB RAM 3600MHz / 1TB NVMe SSD
- Panasonic ToughBook FZ-55-2 11.gen. I5 laptop (Intel I5-1145G7 8X4400 Mhz, 16GB DDR4, 512GB NVME)
- billentyűzetek - kiárusítás - Logitech, Corsair, ASUS
- Apple iPhone 14 Pro 512GB, Kártyafüggetlen, 1 Év Garanciával
- 197 - Lenovo Legion Pro 5 (16IAX10) - Intel Core U7 255HX, RTX 5070 (ELKELT)
- BOMBA ÁRrR! Lenovo ThinkPad T14 Gen 4 Ryzen 5 PRO 7540U 14" FHD+ 32GB 1TB Lenovo gar: 2028.01.07!
- Lenovo L14 G4 Ryzen 5 7530U Gyári Garancia Radeon Vega 16Gb Ram Boltból Számlával
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
