C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 9th, 2012, 07:10 AM
Deadman19 Deadman19 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 Deadman19 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old December 9th, 2012, 12:20 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 2 m 3 sec
Reputation Power: 1774
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";
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Edit a HTML file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap