Praca s textom v C, help needed

Programovacie jazyky, rady, poradňa...
argeus
Novice
Novice
Príspevky: 5
Registrovaný: 15 apr 2011, 10:51

Praca s textom v C, help needed

Príspevok od používateľa argeus »

Ahojte kamarati,
potreboval by som pomoct, poradit, utesit..:D
Potrebujem v C napisat program, ktory nacita textovy subor do pamate (ako retazec), vyhlada podretazec, podla urcitej logiky ho zameni, a vypise do noveho suboru. Podarilo sa. Problem je, ze potrebujem tento novy pozmeneny blok do suboru vypisat povedzme n krat (vzdy s novou hodnotou nahradneho podretazca). Vsetko by to bolo ako tak urobene, len mi to nespravne zapisuje do suboru (a program sa obcas sprava trochu nestabilne). Kod pripajam, rovnako tiez citaci subor..
vstupy: 10, 10, 0010.dat
Dakujem velmi pekne..:)

Citaci subor (journal):

Kód: Vybrať všetko

(cx-gui-do cx-activate-item "MenuBar*ReadSubMenu*Data...")
(cx-gui-do cx-set-text-entry "Select File*Text" "0010.dat")
(cx-gui-do cx-activate-item "Select File*OK")
Kod:

Kód: Vybrať všetko

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *fo, *fw;
    char k, g;
    char *buffer;
    float p;
    int c,i=0;
    char *subs_1, *subs, *pom_pre2, *lalala;
    int subs_e;
    int poz, zn, n;
    int block, period, count; 
    char original[20];
    char dat[] = ".dat";
    char *pom_string;
    int lenght_original, lenght_subs, compar;
 
printf("\n\nEnter:\n");
printf("--------\n");
printf("Number of cycles: \t");
scanf("%d",&count);
printf("Autosave periodicity: \t");
scanf("%d",&period);
printf("Replaced string[XXXX.dat]: \t");
scanf("%s",original);
	
  buffer = (char *)malloc(sizeof(char));

	if (buffer == 0)
	     {
			printf("ERROR: Out of memory\n");
			return 1;
		 }
		 
  char *replace(char *st, char *orig, char *repl)
       {
       char *ch;

       if (!(ch = strstr(st, orig)))
          return st;
       strncpy(buffer, st, ch-st);
       buffer[ch-st] = 0;
       sprintf(buffer+(ch-st), "%s%s", repl, ch+strlen(orig));
       return buffer;
       }

    fo = fopen ( "journal", "r" );
    if (fo == NULL)
         {
          printf ( "cannot open file" );
          return;
         }
                       
    fw = fopen ( "animacia", "a" );
    if ( fw == NULL )
         {
            printf ( "cannot open target file" );
            fclose (fo);
            return;
         }

for(i=1;i<count+1;i++){

subs_e=i*period;

if (subs_e>=10000000.)
   printf("\nERR OVERFLOW\n");
   else if (subs_e>=1000000.)
        n=7;
        else if (subs_e>=100000.)
             n=6;
                 else if (subs_e>=10000.)
                      n=5;
                      else if (subs_e>=1000.)
                           n=4;
                           else if (subs_e>=100.)
                                n=3;
                                else if (subs_e>=10.)
                                     n=2;
                                     else if (subs_e<=9.)
                                          n=1;

poz=0;                                                        
zn=0;                                                         
subs_1 = ecvt(subs_e,n,&poz,&zn);                             
subs=subs_1;                                                  
strcat(subs,dat);                                             
getch();                                                      

lenght_original = strlen (original)-4;
lenght_subs = strlen (subs_1)-4;
compar=lenght_original-lenght_subs;

if (compar<0&&compar>=7)
   printf("\nERR LENGHT\n");
   else if (compar>=6)
        pom_string = strdup("000000");
        else if (compar>=5)
             pom_string = strdup("00000");
             else if (compar>=4)
                  pom_string = strdup("0000");
                  else if (compar>=3)
                       pom_string = strdup("000");
                       else if (compar>=2)
                            pom_string = strdup("00");
                            else if (compar>=1)
                                 pom_string = strdup("0");
                                 else if (compar>=0)
                                      pom_string = strdup("");

lalala=pom_string;
strcat(lalala,subs);

printf("subs_e: %d\n",subs_e);
printf("n: %d\n",n);
printf("original: %s\n",original);
printf("subs_1: %s\n",subs_1);
printf("int_original: %d\n",lenght_original);
printf("int_subs_1: %d\n",lenght_subs);
printf("subs: %s\n",subs);
printf("pom_string: %s\n",pom_string);

while(fgets(buffer,10000000,fo)!=NULL)
  
     {
               puts(replace(buffer,original,lalala));
               fprintf(fw,"%s" ,buffer);
     }

  fclose (fo);
  free(buffer);
  fclose (fw);
  }

  system("pause");
} 






//zdroják zmazný. Prečítaj si pravidlá, a daj ho do code
upravene, vdaka za info, nevedel som.. :smt017.. :)
paralen
Light Star
Light Star
Príspevky: 267
Registrovaný: 02 máj 2006, 15:15

Re: Praca s textom v C, help needed

Príspevok od používateľa paralen »

najprv daj zdrojak do CODE a odsad ho, potom sa na to aj niekto pozrie :)
argeus
Novice
Novice
Príspevky: 5
Registrovaný: 15 apr 2011, 10:51

Re: Praca s textom v C, help needed

Príspevok od používateľa argeus »

pre uplnost, vystup do suboru by mal vyzerat nejako takto:

Kód: Vybrať všetko

(cx-gui-do cx-activate-item "MenuBar*ReadSubMenu*Data...")
(cx-gui-do cx-set-text-entry "Select File*Text" "0010.dat")
(cx-gui-do cx-activate-item "Select File*OK")

(cx-gui-do cx-activate-item "MenuBar*ReadSubMenu*Data...")
(cx-gui-do cx-set-text-entry "Select File*Text" "0020.dat")
(cx-gui-do cx-activate-item "Select File*OK")

(cx-gui-do cx-activate-item "MenuBar*ReadSubMenu*Data...")
(cx-gui-do cx-set-text-entry "Select File*Text" "0030.dat")
(cx-gui-do cx-activate-item "Select File*OK")

(cx-gui-do cx-activate-item "MenuBar*ReadSubMenu*Data...")
(cx-gui-do cx-set-text-entry "Select File*Text" "0040.dat")
(cx-gui-do cx-activate-item "Select File*OK")

a tak dalej..
johny29
Medium Star
Medium Star
Príspevky: 304
Registrovaný: 09 mar 2011, 21:07

Re: Praca s textom v C, help needed

Príspevok od používateľa johny29 »

Caute,
Prepisujem z jedneho txt do druheho. A vtom 2. nechcem zapisat posledne 2 znaky. nejaku radu ako to spravit?
Napísať odpoveď