Ú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!
- Ryzen 7 9700x AM5 CPU - garancia 2028.05. hóig
- Philips Evnia 27M2N8500 QD-OLED monitor 1440p 360Hz ALZA garancia
- MacBook Air (2020) M1 processzor, 8GB RAM, 256 GB kiváló állapotban eladó
- Felújított (Refurbished) DELL Latitude 5440 Tartós Üzleti Laptop 14" -40% i5-1345U 10Mag 16/512 FHD
- Xiaomi Poco Pad 256GB blue
- ÁRGARANCIA! Épített KomPhone Ultra 9 285K 32/64GB RAM RTX 5070 Ti 16GB GAMER PC termékbeszámítással
- 2 TB-os Samsung 980 PRO M.2 SSD with heatsink - garanciával
- iPhone 17 256 GB Black - Bontatlan !! www.stylebolt.hu - Apple eszközök és tartozékok - Számlás
- Bomba ár! Dynabook Portege X30-L - i5-1135G7 I 8-16GB I 256SSD I 13,3" FHD I Cam I W11 I Garancia!
- LG NanoCell 43NANO763QA csere is érdekel (dobozával minden tarozéka meg van)
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest

