Diskuze: string + subot
Zobrazeno 4 zpráv z 4.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V čistom c by to mohlo byť napr. takto:
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *fw;
char buffer[101], veta[10000];
if ((fw = fopen("vety.txt", "w")) == NULL) {
printf("Subor sa nepodarilo otvorit.\n");
return 1;
}
while (1) {
printf("Zadajte vetu na vlozenie do suboru: ");
scanf(" %100[^\n]", buffer);
if (strstr(buffer, "KONIEC SUBORU") == 0) {
strcat(veta, buffer);
strcat(veta, "\n");
}
else
break;
}
fprintf(fw, "%s", veta);
if (fclose(fw) == EOF) {
printf("Subor sa nepodarilo zavriet.\n");
return 1;
}
return 0;
}
ja som to nakoniec vyriesil takto ....chcel by som sa spýtať na prikazy gets a fputs.. či je na nich niečo zle lebo niekde som čítal že neodporučaju ich pouzivať. a tiež som niekde čital že je lepšie použivat na infinite loop for ako while ...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buffer[1024];
FILE *subor = fopen("vety.txt", "w");
for (;;)
{
printf("Zadaj vetu.Pre ukoncenie zadaj KONIEC SUBORU \n");
gets(buffer);
if (strstr(buffer,"KONIEC SUBORU")!=0)
break;
fputs(buffer,subor);
fputs("\n",subor);
}
fclose(subor);
return 0;
}
fputs() sa dá použiť iba s reťazcami.
fprintf() má celý rad formátovacích zariadení pre niekoľko typov dát
Citácia z stackoverflow.com:
Uhm... ...puts() just writes a string, while printf() has a number of formatting
facilities for several types of data.
fputs() http://www.cplusplus.com/…stdio/fputs/
fprintf() http://www.cplusplus.com/…dio/fprintf/
Zobrazeno 4 zpráv z 4.