- Milyen asztali (teljes vagy fél-) gépet vegyek?
- Milyen RAM-ot vegyek?
- Lenovo Legion és IdeaPad Y széria
- Vezeték nélküli fülhallgatók
- AMD Ryzen 9 / 7 / 5 9***(X) "Zen 5" (AM5)
- NVIDIA® driverek topikja
- Fejhallgató erősítő és DAC topik
- Gaming notebook topik
- Milyen alaplapot vegyek?
- Azonnali alaplapos kérdések órája
Ú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!
- ÁRGARANCIA!Épített KomPhone i5 14600KF 16/32/64GB RAM RTX 5070 12GB GAMER PC termékbeszámítással
- Sima Vs.Windows Logitech Mx keys s plus és hagyományos Mx keys magyar bemutatása. Új videó linkel
- Apple iMac 19.2 i5-8500 Radeon Pro 560X 4GB 16GB 256GB SSD 21.5" 4K Retina
- GYÖNYÖRŰ iPhone Xs Max 256GB Black -1 ÉV GARANCIA - Kártyafüggetlen, MS3537, 100% Akkumulátor
- Bomba ár! Dell XPS 15 9550 - i7-HQ I 16GB I 512SSD I 15,6" FHD I GTX960M I Cam I W10 I Gari!
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest