473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem in text management while file handling

9
hi all...this is urgent..i need to rearrange the content of a text file which looks like:

Terminal log file
Date: 21/11/2006 - 6:17:04 PM
-----------------------------------------------
01 02 01 01 00 4B 00 38 46 24 12 01 02 01 01 00
4B 00 38 46 24 12 01 02 01 01 00 4B 00 38 46 24
12 01 02 01 01 00 4B 00 38 46 24 12 01 02 01 01
00 4B 00 38 46 24 12 01 02 01 01 00 4B 00 38 46
24 12 01 02 01 01 00 4B 00 38 46 24 12 01 02 01
01 00 4B 00 38 46 24 12 01 02 01 01 00 4B 00 38
46 24 12 01 02 01 01 00 4B 00 38 46 24 12 01 02
01 01 00 4B 00 38 46 24 12 01 02 01 01 00 4B 00
38 46 24 12 01 02 01 01 00 4B 00 38 46 24 12 01
02 01 01 00 4B 00 38 46 24 12 01 02 01 01 00 4B
00 38 46 24 12 01 02 01 01 00 4B 00 38 46 24 12
--------------------------------------------------------------------------


i've to arrange it like: and save it in a new file in the format:

Terminal log file
Date: 21/11/2006 - 6:17:04 PM
-----------------------------------------------
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12

i cant maintain the required alignment. i've written the following code in C AND I'M USING A TURBO C ASSEMBLER. Please suggest me the required corrections in my code. Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  #include<stdio.h>
  2.       #include<conio.h>
  3.  
  4.       void main (void)
  5.       {FILE *f1, *f2;
  6.        char n;
  7.        int i,k;
  8.  
  9.        clrscr();
  10.  
  11.        f1 = fopen("c:\\turboc3\\a.log","r+");
  12.        if(f1==NULL)
  13.        {printf("cant open file");
  14.     getch();
  15.        //    exit();
  16.        }
  17.        f2 = fopen("c:\\turboc3\\N.log","w+");
  18.  
  19.        for(i=0;i<96;i++)
  20.        {n=getc(f1);
  21.        putc(n,f2);
  22.        printf("%c",n);
  23.        }
  24.  
  25.  
  26.        if(kbhit())
  27.         {
  28.          if((n=getch())==27)
  29.          exit (0);
  30.         }
  31.        else
  32.         {while((n=getc(f1))!= EOF)
  33.           {
  34.           for(k=0;k<=47;k++)
  35.         {
  36.            for (i=0;i<32;i++)
  37.             { n = getc(f1);
  38.               if(n =='\n')
  39.             { fprintf(f2,"");
  40.               printf(" ");
  41.             }
  42.               else
  43.             { fprintf(f2,"%c",n);
  44.               printf("%c",n);
  45.             }
  46.            }
  47.            fprintf(f2,"\n");
  48.         }
  49.           fprintf(f2," ") ;
  50.           }
  51.       }
  52.  
  53.  
  54.  
  55.      fclose(f1);
  56.      fclose(f2);
  57.  
  58.      getch();
  59.     }
  60.  
if somebody wants..i can post the actual file that i am using....
Dec 7 '06 #1
9 1465
horace1
1,510 Expert 1GB
perhaps your requirement is more complicated than it looks but it I think a much simpler program would do what you require, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.       #include<conio.h>
  3.  
  4.       int main (void)
  5.       {FILE *f1, *f2;
  6.        char n;
  7.        f1 = fopen("a.log","r+");
  8.        if(f1==NULL)
  9.        {printf("cant open file");
  10.     getch();
  11.        //    exit();
  12.        }
  13.        f2 = fopen("N.log","w+");
  14.      int count=0;
  15.      while((n=getc(f1))!= EOF)
  16.           {
  17.            if(n < ' ') n= ' ';  // replave \n with a space
  18.            fprintf(f2,"%c",n);  // output the character
  19.            printf("%c", n);
  20.            // if at end of line output a \n and reset count
  21.            if(++count == 33) { printf("\n"); count=0; }
  22.           }
  23.      fclose(f1);
  24.      fclose(f2);
  25.      getch();
  26.     }
  27.  
Dec 7 '06 #2
ajain
9
perhaps your requirement is more complicated than it looks but it I think a much simpler program would do what you require, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.       #include<conio.h>
  3.  
  4.       int main (void)
  5.       {FILE *f1, *f2;
  6.        char n;
  7.        f1 = fopen("a.log","r+");
  8.        if(f1==NULL)
  9.        {printf("cant open file");
  10.     getch();
  11.        //    exit();
  12.        }
  13.        f2 = fopen("N.log","w+");
  14.      int count=0;
  15.      while((n=getc(f1))!= EOF)
  16.           {
  17.            if(n < ' ') n= ' ';  // replave \n with a space
  18.            fprintf(f2,"%c",n);  // output the character
  19.            printf("%c", n);
  20.            // if at end of line output a \n and reset count
  21.            if(++count == 33) { printf("\n"); count=0; }
  22.           }
  23.      fclose(f1);
  24.      fclose(f2);
  25.      getch();
  26.     }
  27.  

thanx sir...perhaps the code is good...but it doesn't gives the rightly formatted output....do u hav ne clue where m i going wrong....?
Dec 10 '06 #3
horace1
1,510 Expert 1GB
thanx sir...perhaps the code is good...but it doesn't gives the rightly formatted output....do u hav ne clue where m i going wrong....?
the screen output was OK but I did not output a newline to the n.log file

program should be
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main (void)
  5. {
  6.     FILE *f1, *f2;
  7.     int count=0;
  8.     char n;
  9.     f1 = fopen("a.log","r+");
  10.     if(f1==NULL)
  11.        {printf("cant open file");
  12.           getch();
  13.            return 1;;
  14.        }
  15.      f2 = fopen("N.log","w+");
  16.      while((n=getc(f1))!= EOF)
  17.           {
  18.            if(n < ' ') n= ' ';  // replave \n with a space
  19.            fprintf(f2,"%c",n);  // output the character
  20.            printf("%c", n);
  21.            // if at end of line output a \n and reset count
  22.            if(++count == 33) { printf("\n"); fprintf(f2, "\n"); count=0; }
  23.           }
  24.     fclose(f1);
  25.     fclose(f2);
  26.     getch();
  27.     return 0;
  28. }
  29.  
when I run using Turbo C with your data the n.log file contains
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12

is that correct?
Dec 10 '06 #4
ajain
9
thnx i checked the code...it is absolutely alrite...i made a new log file and tried running it and it worked well , but it is not running on my actual log data.
is it possible that i show u the actual log sheet, its a long .log file
wwll also cud u tell me the meaning of this line of code that u used?

Expand|Select|Wrap|Line Numbers
  1. if(n < ' ') n= ' ';  // replave \n with a space
  2.  
the screen output was OK but I did not output a newline to the n.log file

program should be
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main (void)
  5. {
  6.     FILE *f1, *f2;
  7.     int count=0;
  8.     char n;
  9.     f1 = fopen("a.log","r+");
  10.     if(f1==NULL)
  11.        {printf("cant open file");
  12.           getch();
  13.            return 1;;
  14.        }
  15.      f2 = fopen("N.log","w+");
  16.      while((n=getc(f1))!= EOF)
  17.           {
  18.            if(n < ' ') n= ' ';  // replave \n with a space
  19.            fprintf(f2,"%c",n);  // output the character
  20.            printf("%c", n);
  21.            // if at end of line output a \n and reset count
  22.            if(++count == 33) { printf("\n"); fprintf(f2, "\n"); count=0; }
  23.           }
  24.     fclose(f1);
  25.     fclose(f2);
  26.     getch();
  27.     return 0;
  28. }
  29.  
when I run using Turbo C with your data the n.log file contains
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12
01 02 01 01 00 4B 00 38 46 24 12

is that correct?
Dec 10 '06 #5
horace1
1,510 Expert 1GB
thnx i checked the code...it is absolutely alrite...i made a new log file and tried running it and it worked well , but it is not running on my actual log data.
is it possible that i show u the actual log sheet, its a long .log file
wwll also cud u tell me the meaning of this line of code that u used?

Expand|Select|Wrap|Line Numbers
  1. if(n < ' ') n= ' ';  // replave \n with a space
  2.  
can you attach or email the log file?

the line
Expand|Select|Wrap|Line Numbers
  1. if(n < ' ') n= ' ';  // replave \n with a space
  2.  
should read
Expand|Select|Wrap|Line Numbers
  1. if(n < ' ') n= ' ';  // replace control characters  with a space
  2.  
any character with a character code less than ' ' (space) are replaced with a ' ', i.e. it is to remove newline, etc.
Dec 10 '06 #6
ajain
9
yes i can mail u the xact log file.but i dont find an option to attach files here.

can you attach or email the log file?

the line
Expand|Select|Wrap|Line Numbers
  1. if(n < ' ') n= ' ';  // replave \n with a space
  2.  
should read
Expand|Select|Wrap|Line Numbers
  1. if(n < ' ') n= ' ';  // replace control characters  with a space
  2.  
any character with a character code less than ' ' (space) are replaced with a ' ', i.e. it is to remove newline, etc.
Dec 10 '06 #7
ajain
9
i am sorry for the second post,
sir i hav mailed u the log files at the adress specified in ur profile.plz confirm if recieved.
Dec 10 '06 #8
horace1
1,510 Expert 1GB
i am sorry for the second post,
sir i hav mailed u the log files at the adress specified in ur profile.plz confirm if recieved.
The problem is dealing with text before and after the numerical data. You have to look for it and output it to the output file unchanged. Lines with numeric data are formatted as required.

try this version
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include <ctype.h>
  4.  
  5. int main (void)
  6. {
  7.     FILE *f1, *f2;
  8.     int count=0;
  9.     int checkDigit=1;
  10.     char n;
  11.     f1 = fopen("a.log","r+");
  12.     if(f1==NULL)
  13.        {printf("cant open file");
  14.           getch();
  15.            return 1;;
  16.        }
  17.      f2 = fopen("N.log","w+");
  18.      while((n=getc(f1))!= EOF)
  19.           {
  20.            if(n == '\n') // if newline check next character is a digit
  21.               { 
  22.                // if last line was non digital output a newline
  23.                if(checkDigit > 0) { printf("\n"); fprintf(f2, "\n"); }
  24.                checkDigit=1;    // check next character is a digit
  25.               }
  26.            if(n >= ' ')           // if not a control character
  27.              {
  28.               fprintf(f2,"%c",n);  // output the character
  29.               printf("%c", n);
  30.               // if last character was a newline check if this character is digit
  31.               if(checkDigit == 1) 
  32.                     // if is is NOT set CheckDigit = 2 else 0
  33.                    if(!isdigit(n)) checkDigit=2; else checkDigit =0;
  34.               // if outputing digits check if we need a newline
  35.               if(checkDigit == 0)
  36.                  // if at end of line output a \n and reset count
  37.                  if(++count == 33) { printf("\n"); fprintf(f2, "\n"); count=0; }
  38.              }
  39.          }
  40.     fclose(f1);
  41.     fclose(f2);
  42.     getch();
  43.     return 0;
  44. }
  45.  
Dec 10 '06 #9
ajain
9
thanks horace, thats an impeccable code. Has very well solved my purpose.
Dec 11 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jake | last post by:
Hi All, > Hi experts, > > I am trying to build a page that allows file uploading from a browser > My ISP where the page will be located, only supports Smartupload > I am testing on a small...
1
by: Abelardo Vacca | last post by:
Hello, I am currently in the process of switching our application to a N-Tier model with .NET. One of the aspects we want ot get right from the start not to worry about it after is the...
6
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the...
5
by: Karl | last post by:
Hi, I have some code that will save the contents of a Rich Text Box in either a Text or Rich Text Format file. The code is using the SaveFileDialog and is working correctly. I have been...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
0
by: JACompute | last post by:
I have an invoicing system that uses a VB6 program to post new invoice entries to a centralized Vendor management system (also in VB6), via a Web server and some ASP code. The system has been in...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
2
by: jedicks | last post by:
Hi I am new to c#. I hava a problem which i am struggling to solve. Please look at code below: StreamReader sr = new StreamReader(sFilename); string line = sr.ReadLine(); ...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
2
by: BobLewiston | last post by:
Some of you may have seen my earlier thread “PasswordHash NULL problem”. I’ve started a new thread because investigation has shown that the problem is actually quite different than I previously...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.