Ahoj všem
Mám takovej problém s tím to prográmkem.Jedná se o program,který projde vstupní HTML soubor a vytvoří jeho kopii obsahující text všech sudých značek v obráceném pořadí,ale mám tady problém,ze mi to proste nebezi a vubec nevim proc!Dekuju za pomoc
Např. <ALESPON>něco<NECO>nebo<B>tučně<DALSI>
Vrátí: <ALESPON>něco<OCEN>nebo<B>tučně<ISLAD>
//---------------------------------------------------------------------------
#include <stdafx.h>
#include <vcl.h>
#include <string.h>
#include <stdio.h>
char *input="c:\\Documents and Settings\\Martin\\Dokumenty\\pokus1.htm";
char *ouput="c:\\Documents and Settings\\Martin\\Dokumenty\\bucta.htm";
// Does not work with html comments
// Does not work with nested items
int main(int argc, char* argv[])
{
FILE *fi=fopen(input,"rb"); // open the file
if(!fi)return 0; // stop on failiure
fseek(fi,0,SEEK_END); // go to the end
int len=ftell(fi); // length of the file
fseek(fi,0,SEEK_SET); // go back to the beginning
char *buf=new char[len+1]; // buffer for the input, one longer than the data
fread(buf,1,len,fi); // read the buffer
buf[len]=0; // make sure that the buffer is null-terminated
fclose(fi); // and close the file
fi=fopen(ouput,"w"); // open the file for writing
if(!fi)return 0; // stop on failiure
char *p;
int i;
for(i=0,p=strtok(buf,"<>");p; p=strtok(0,"<>"),i++) // split buf on < and >
{
if(i%2==0) // odd chunk must be <>
{ fputs("<", fi); // so put <
if(i%4)p=strrev(p); // if even in odds, that reverse
}
fputs(p,fi); // put the string
if(i%2==0)fputs("<", fi); // if odd close the >
}
fclose(fi); // and close the file
return 0;
}
//------------------------------------------------------------------------------
Fórum › C / C++
Reverzace znaku v HTML souboru

Huh, k cemu je to dobry 8-)
Nejak nechapu, co ti na tom nechodi, ale muzes zkusit tohle:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define FILE_TO_READ "in.txt"
#define FILE_TO_WRITE "out.txt"
int main(int argc, char *argv[])
{
FILE *fr = fopen(FILE_TO_READ, "r"), *fw = fopen(FILE_TO_WRITE, "w");
if(!fr || !fw) return -1;
char buf[512], *pch;
int c = 0;
while(fgets(buf, 512, fr))
for(pch = buf; *pch;++pch)
{ if(*pch == '<') ++c;
if(*pch == '<' && c && c%2 == 0)
{ char *p = strchr(pch, '>');
if(!p) continue;
char buf[512];
strncpy(buf, pch+1, p-pch-1); buf[p-pch-1]='\0';
fprintf(fw,"<%s>", strrev(buf));
pch = p;
}
else
fprintf(fw,"%c", *pch);
}
fclose(fr);
fclose(fw);
system("PAUSE");
return 0;
}
Ale pozor, vzhledem k pouzitemu nacitani po radcich si to neporadi s vice radkovymi tagy. Pokud je to moc velka nevyhoda, tak si podobne jako vyse nahrad fgets alokovanim a nactenim celeho soubrou do pameti nebo to cti znak po znaku ze souboru.
Bucta píše:#
# To Zelenáč:Jo hochu to je moje uloha do skoly,ale mam s tim fakt problem!
Asi vam zcvoknul ucitel...
no niekedy dojdu ulohy tak su na rade taketo blbosti.
na konci ročníka mame robiť nejaku záverečnu prácu v C. Nemate niekto nejaky jednoduchy napad , ktory vizera zložito ? :)
To Bucta:
VCL knihovna je veci borlandu, tudiz pokud to prekldas pod gcc, nebo jakymkoliv jinym prekladacem, tak se neni cemu divit, ze kompilator nezna vcl.h. A reseni? Smaz ten radek(kdyby jsi nevedel: #include <vcl.h>)
mephi píše:#
# no niekedy dojdu ulohy tak su na rade taketo blbosti.#
##
#na konci ročníka mame robiť nejaku záverečnu prácu v C. Nemate niekto nejaky jednoduchy napad , ktory vizera zložito ? :)
Stahnout linux a CTRL+C ;-)
Kdyz ti delalo problemy zkompilovat tohle reseni, tak proc jsi neupravil a nepouzil me??
#include <string.h>
#include <stdio.h>
char *input="c:\\Documents and Settings\\Martin\\Dokumenty\\pokus1.htm";
char *ouput="c:\\Documents and Settings\\Martin\\Dokumenty\\bucta.htm";
// Does not work with html comments
// Does not work with nested items
int main(int argc, char* argv[])
{
FILE *fi=fopen(input,"rb"); // open the file
if(!fi)return 0; // stop on failiure
fseek(fi,0,SEEK_END); // go to the end
int len=ftell(fi); // length of the file
fseek(fi,0,SEEK_SET); // go back to the beginning
char *buf= (char*)malloc(sizeof(char)*(len+1));//new char[len+1]; // buffer for the input, one longer than the data
fread(buf,1,len,fi); // read the buffer
buf[len]=0; // make sure that the buffer is null-terminated
fclose(fi); // and close the file
fi=fopen(ouput,"w"); // open the file for writing
if(!fi)return 0; // stop on failiure
char *p;
int i;
for(i=0,p=strtok(buf,"<>");p; p=strtok(0,"<>"),i++) // split buf on < and >
{
if(i%2==0) // odd chunk must be <>
{ fputs("<", fi); // so put <
if(i%4)p=strrev(p); // if even in odds, that reverse
}
fputs(p,fi); // put the string
if(i%2==0)fputs("<", fi); // if odd close the >
}
fclose(fi); // and close the file
free(buf);
return 0;
}

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
četnost znaků v souboru - C — založil Petra
Výběr prvních 50 znaků souboru — založil peetzn
Načtení znaků ze souboru do matice — založil Sprinter
ŠPatné počítání znaků v souboru — založil chef06
Serializace do souboru širokých znaků — založil Porcepoque
Moderátoři diskuze