The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Edit a HTML file
Discuss Edit a HTML file in the C Programming forum on Dev Shed. Edit a HTML file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 9th, 2012, 07:10 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 1
Time spent in forums: 8 m 7 sec
Reputation Power: 0
|
|
|
Edit a HTML file
Hey, guys. I need a little help here. I have to edit a HTML text file in C and my first task is to put every tag and the text between them on a new line. So far I managed to do that everytime I find an ">", but I can't do the same thing for the "<" character. Here is the source:
Quote: #include <stdio.h>
#include <string.h>
void strins (char *d, char *s)
{
char *aux=strdup(d);
strcpy(d,s);
strcat(d,aux);
}
void verif1(char *a, char b[10])
{
char *poz;
poz=strchr(a,'>');
while (poz!=NULL)
{
if(a[poz-a+1]!='\n' && a[poz-a+1]!='\0')
{
strins(a+(poz-a+1),b);
a=(char *)realloc(a,strlen(a)*sizeof(char));
}
poz=strchr(poz+1,'>');
}
}
void verif2(char *a, char b[10])
{
char *poz;
poz=strchr(a,'<');
while (poz!=NULL)
{
if((poz-a)!=0 && a[poz-a-1]!='\n')
{
strins(a+(poz-a-1),b);
a=(char *)realloc(a,strlen(a)*sizeof(char));
}
poz=strchr(poz+1,'<');
}
}
int main()
{
freopen("date.in","r",stdin);
char *a, b[10];
int i=0;
b[i]='\n';
b[i+1]='\0';
a=(char *)malloc(40*sizeof(char));
while (!feof(stdin))
{
a[i]=getc(stdin);
i++;
}
a[i]='\0';
verif1(a,b);
verif2(a,b);
printf("%s \n",a);
return 0;
} |
The verif1 function is for the ">" character and verif2 is for "<". Can anybody tell me what might be the problem?
|

December 9th, 2012, 12:20 PM
|
 |
Contributed User
|
|
|
|
Allow me to indent your code...
Code:
#include <stdio.h>
#include <string.h>
void strins(char *d, char *s)
{
char *aux = strdup(d);
strcpy(d, s);
strcat(d, aux);
}
void verif1(char *a, char b[10])
{
char *poz;
poz = strchr(a, '>');
while (poz != NULL) {
if (a[poz - a + 1] != '\n' && a[poz - a + 1] != '\0') {
strins(a + (poz - a + 1), b);
a = (char *) realloc(a, strlen(a) * sizeof(char));
}
poz = strchr(poz + 1, '>');
}
}
void verif2(char *a, char b[10])
{
char *poz;
poz = strchr(a, '<');
while (poz != NULL) {
if ((poz - a) != 0 && a[poz - a - 1] != '\n') {
strins(a + (poz - a - 1), b);
a = (char *) realloc(a, strlen(a) * sizeof(char));
}
poz = strchr(poz + 1, '<');
}
}
int main()
{
freopen("date.in", "r", stdin);
char *a, b[10];
int i = 0;
b[i] = '\n';
b[i + 1] = '\0';
a = (char *) malloc(40 * sizeof(char));
while (!feof(stdin)) {
a[i] = getc(stdin);
i++;
}
a[i] = '\0';
verif1(a, b);
verif2(a, b);
printf("%s \n", a);
return 0;
}
Here are some problems.
1. strins leaks the memory allocated to aux
2. your realloc calls are shrinking memory, not growing memory. strlen() tells you the length of the string excluding the \0. You need to count the \0 AND allow for the insertion of one more character.
General comments.
1. Better variable names than a, b, s would go a long way to improving readability.
2. Expressions like a[poz - a + 1] can be reduced to poz[1]
3. Don't cast the return result of realloc/malloc. All it is doing in your case is masking the fact that you've failed to include stdlib.h.
4. The initial allocation of 40 chars is inadequate for any meaningful HTML document.
5. Your use of feof() is wrong - see here
6. The b variable in main could be reduced to char *b = "\n";
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|