C - zápis do súboru

C,C++,C#

Moderátori: psichac, Moderátori

ma_to
Nový člen
Nový člen
Príspevky: 77
Dátum registrácie: 28 Feb 2016, 10:56
Bydlisko: Púchov

C - zápis do súboru

Príspevok od používateľa ma_to » 08 Dec 2017, 21:14

Zdravím na internete som našiel tento kód, sám o sebe funguje perfektne, problém nastane keď ho zabalím do if alebo switchu. Mohol by mi niekto vysvetliť prečo sa to deje, poprípade navrhnúť opravu ? Ďakujem

Kód: Vybrať všetko

#include <stdio.h>
int main()

{
 FILE *fp;
 char filename[100];
 char writestr[100];

  // Read filename
  printf("Enter a filename :");
  gets(filename);

  // Read string to write
  printf("Enter the string to write :");
  gets(writestr);

   // Open file in write mode
   fp = fopen(filename,"w+");

   // If file opened successfully, then write the string to file
   if ( fp )
   {
	   fputs(writestr,fp);
    }
   else
      {
         printf("Failed to open the file\n");
        }
//Close the file
   fclose(fp);
return(0);
}
0

Používateľov profilový obrázok
jirka.jirka.
Ultimate člen
Ultimate člen
Príspevky: 1537
Dátum registrácie: 17 Okt 2008, 00:00
Bydlisko: Uherské Hradiště
Kontaktovať používateľa:

Re: C - zápis do súboru

Príspevok od používateľa jirka.jirka. » 08 Dec 2017, 21:21

Ukaž jak to balíš do toho if, nebo switch.
0

ma_to
Nový člen
Nový člen
Príspevky: 77
Dátum registrácie: 28 Feb 2016, 10:56
Bydlisko: Púchov

Re: C - zápis do súboru

Príspevok od používateľa ma_to » 08 Dec 2017, 22:55

Kód: Vybrať všetko

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int i;
int main()
{

    printf("Zadaj 1: ");
    scanf("%d", &i);
    if(i == 1){

 FILE *fp;
 char filename[100];
 char writestr[100];

  // Read filename
  printf("Enter a filename :");
  gets(filename);

  // Read string to write
  printf("Enter the string to write :");
  gets(writestr);

   // Open file in write mode
   fp = fopen(filename,"w+");

   // If file opened successfully, then write the string to file
   if ( fp )
   {
	   fputs(writestr,fp);
    }
   else
      {
         printf("Failed to open the file\n");
        }
//Close the file
   fclose(fp);
return(0);
    }
}
0

Používateľov profilový obrázok
zen
Stály člen
Stály člen
Príspevky: 483
Dátum registrácie: 22 Nov 2013, 14:33
Bydlisko: Cadca

Re: C - zápis do súboru

Príspevok od používateľa zen » 09 Dec 2017, 01:55

Problem nie je v if. Toto funguje:

Kód: Vybrať všetko

#include <stdio.h>
void main() {
  int i;
  char dummy;
  printf("Zadaj 1: ");
  scanf("%d%c",&i,&dummy);
  if(i==1){
    FILE *fp;
    char filename[100];
    printf("Enter a filename: "); gets(filename);
    fp=fopen(filename,"w+");
    if (fp) {fputs(filename,fp); fclose(fp);}
    else    {printf("Failed to open %s",filename);}
  }
return;}
K&R: scanf(), v Second Edition page 159.
0
Samozřejmě, že dědeček nemá kapesní hodinky poháněné elektřinou. Takový nesmysl si může vymyslit jen Saturnin. (Zdenek Jirotka, 1942)

ma_to
Nový člen
Nový člen
Príspevky: 77
Dátum registrácie: 28 Feb 2016, 10:56
Bydlisko: Púchov

Re: C - zápis do súboru

Príspevok od používateľa ma_to » 09 Dec 2017, 14:00

Super, ďakujem :applause:
0

Napísať odpoveď
  • Podobné témy
    Odpovedí
    Zobrazení
    Posledný príspevok