GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Nov-2004, 10:55
kensenb kensenb is offline
New Member
 
Join Date: Nov 2004
Posts: 6
kensenb is on a distinguished road
Angry

converting text files to html files


What are source and destination files. I need an idea on writing a code in c programming in which I have to convert text files to html files. Basically I have to read through each line for specific text tags, that needs to be changed to html tags. Please help :-x
  #2  
Old 07-Nov-2004, 11:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by kensenb
What are source and destination files. I need an idea on writing a code in c programming in which I have to convert text files to html files. Basically I have to read through each line for specific text tags, that needs to be changed to html tags. Please help :-x


The source file is the input to your program (the text file, I guess).

The destination file is the output from your program.



The program:
Code:
General flow of most programs: input ==> process ==> output. Specific for this program: Open the input file for reading; open the output file for writing. Repeat the following three steps as long as more lines are available from the input file: 1. Read a line from the input file. 2. Process the line for whatever changes are required. 3. Write the modified line to the output file. Close the input file and the output file.


Regards,
Dave
  #3  
Old 07-Nov-2004, 11:40
kensenb kensenb is offline
New Member
 
Join Date: Nov 2004
Posts: 6
kensenb is on a distinguished road

convert text to html


Thanks Dave, but how do you setup two files as a source and destination file. Also how do you take text from one file and write it to another file.

Thanks Ken
  #4  
Old 07-Nov-2004, 13:32
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
You first have to declare a FILE* variable as a handle to the file you want to open, then you must assign the handle to a file and open it via the fopen() command (there are others, but fopen() is probably the most common in console programming). You also must specify how you want to open it, if you want to read from it, write to it, append to it, etc. After opening you then need to check for errors, and if it opened correctly. Once opened, you typically read in a file with the fread(), or write via the fwrite() functions. There are others, but these are the fastest and most efficient. You then need to remember to close the file with fclose(). A simple file reader would look something like this:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
char buffer[1000], char infile[MAX_PATH]; //Declare char arrays, MAX_PATH is 256
FILE *in; //Declare FILE pointer

printf("Enter a file to read:\n"); //print instuctions
gets(infile); //Get the file to be read

in = fopen(infile, "rb"); //Assign in pointer to specified file, r stands for read, and b stand for binary, not always necessary, I just do it out of habit
if(in == NULL) {printf("Inavilid file name"); return 1;} //Check for errors

//Read in the file, 1st argument=string to read to
//2nd argument=I forget, almost always set to 1 though
//3rid argument=how many characters to read in
//4rth argument=file handle to read from
fread(buffer, 1, 1000, in);
puts(buffer); //prints string to stdout

fclose(in); //don't forget to close file

return 0;
}
Usually when reading files, one would specify a char pointer and then allocate memory based on file size before reading, then free it up afterwards.

Anyways, hope it helped.
  #5  
Old 08-Nov-2004, 07:06
kensenb kensenb is offline
New Member
 
Join Date: Nov 2004
Posts: 6
kensenb is on a distinguished road
This is the last question I have about this certain task. How do I actually go about processing each line of the input file for changes I need to make to this file.

Thanks Ken
  #6  
Old 08-Nov-2004, 10:11
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
You can scan the buffer you read in for the escape characters/flags you need to change. Some like:
CPP / C++ / C Code:
//buffer is read in file...
//Declare variables
char *outbuf;
int i = 0;

outbuf = (char*)malloc(/*whatever size needed*/); //allocate memory
if(outbuf == NULL) {perror("Memory Error"); return 1;} /*check for errors, perror() adds a colon and space to the string you give it, then prints the most recent error*/

while(i</*length of file read in or strlen(buffer)*/)
{
if((buffer[i] == '\r') && (buffer[i+1] == '\n')) //if newline
{
strcat(outbuf, "<br>");
i+=2;
}
else if(buffer[i] == '\n') //if other type of newline
{
strcat(outbuf, "<br>");
i++;
}
else if(buffer[i] == '\t') //if tab
{
//You'll need additional processing to tell when to open and close the <p> tag.
strcat(outbuf, "<p>");
i++;
}
//more flags...
else //if not a tag
{
strcat(outbuf, buffer[i]);
i++;
}
}
//...

It's not the most efficient code in the world, but it should work.
  #7  
Old 08-Nov-2004, 10:30
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Or you can simply add following line in your code to read the file line by line.

CPP / C++ / C Code:

char line[1000];
char buffer[1000];

while (fgets(buffer,sizeof(buffer),fpin)!=EOF)  //Where "fpin" is an input file pointer
{
  /* Process the string captured from file in char array "buffer" */
      
      sprintf(line,"<b> %s </b>", buffer);  
 /* now "line" contains your html formatted line. Write it to your new file */
  
      fprintf(fpout,"%s\n",line); //Where "fpout" is an output file pointer


}

  #8  
Old 08-Nov-2004, 12:47
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
You can use the fgets() method, but I wouldn't do the sprintf() stuff which you implemented. What you're basically doing is double spacing every line, where all you want to do is to put in a line break tag whenever you come accross a newline escape character.
  #9  
Old 08-Nov-2004, 16:36
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
You are right Dr. Evil,

I was just trying to give a general example as to how one can read from the file and modify the content (with HTML Tags) and write the new formatted line into a new file. If he just needs to add a new line he can also do

CPP / C++ / C Code:
char line[1000];
char buffer[1000];

while (fgets(buffer,sizeof(buffer),fpin)!=EOF)  //Where "fpin" is an input file pointer
{
  /* Process the string captured from file in char array "buffer" */
      
      sprintf(line,"%s<br>", buffer);  
 /* now "line" contains your html formatted line. Write it to your new file */
  
      fprintf(fpout,"%s",line); //Where "fpout" is an output file pointer


}

Does this look fine? I am sorry, but I am a bit rusty with HTML scripting.
Moreover, his initial post did not mention what kind of formating he needs to do with HTML. He can do whole lots of processing in that while loop , on captured buffer before writing it into the new html file. This is just a sample.
  #10  
Old 08-Nov-2004, 16:43
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

I forgot to mention


One thing I forgot to mention is that, fgets will also copy the newline character '\n' from the file into the buffer. So you need to rip it off before you process the line.

CPP / C++ / C Code:
buffer[strlen(buffer)-1]='\0';

HTH.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
phpmyadmin--what format for text files to upload? bufhal MySQL / PHP Forum 0 04-Jul-2004 11:07
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26
Manipulating text Files k209310 C++ Forum 0 17-Nov-2003 11:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.