Question

READING FROM A FILE

Asked by: sanko50

hi,
     i  have a data file which contains data like below.....

   5 -1 -2  0 0 0 0 0 0 0 0 0 0 0 0 4 6 7 0 0 0 1 2 3 ..........

i want a CODED OUTPUT in a file like below.......

  5 -1 -2 *012* 4 6 7 0 0 0 1 2 3....  

i am finding basically where the repetion is....

*012* means
------------------
*--->starting flag(for repetation)
0 12 ----> character 0 is repeating 12 times

*--->end of repetation flag

like this way i want to code my input.....

N.B if any character is repeating less than three times i will not code that character

can you give some algorithm/pseuodocode/  any outline to start with????

i usued while loop and was reading two character at a time  and compared ....but i was  not able to get my desired result ....

can you give SIMPLE  STEPS how to do it????

thanks

N.B plz let me know if i am not able to make you understand my problem....thanks

 

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-01-11 at 22:34:00ID20847952
Tags

pseuodocode

,

file

,

repetation

Topic

C Programming Language

Participating Experts
3
Points
30
Comments
16

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Algorithm
    I want to develop an algorithm that solves the Y2k problem by changing all the y's to k's in a documents.(can be assumed that a document is merely and array of characters) What are good especifications for it?
  2. big problem plz help...
    hello, can someone help me... i whant that my programme does more things ad the same time. someone tolled me it's possible and it's called "threats". plz give me information about this or a example or something plz help... carlos smith
  3. Outline Object
    I'm working with a set of records in a datatable that have to be organized in a rigorous outline, with each record having a "parent" and potentially "children" and "siblings". The "siblings" have to be maintained in a particular sequen...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: sunnycoderPosted on 2004-01-11 at 22:40:47ID: 10093754

Hi sanko50,

char last_char;
int count=0;
char string[] = "0 0 0 0 0 1 2 2 0 0";
char * temp = string;
last_char = * temp;

/* loop over input begin */

while ( * temp == last_char )
{
     count++;
     temp = temp+2; //+2 for intervening space
}

if ( count > 3)
     /* print * last_char count*  */
     printf ( "*%c%d*",last_char,count);
else
     /* print last_char count times */

last_char = *temp;
count = 0;

/* loop over input end*/

I hope you can fill in the blanks

Cheers!
Sunny:o)

 

by: ssnkumarPosted on 2004-01-11 at 23:21:28ID: 10093836

> if ( count > 3)
It should be changed to "if ( count > 2)". You are starting from 0 and he wants anything more than 3!

And sanko50, sunnycoder has not given code for reading from a file. Hope you know that part already! Anyway, I will give steps to achieve this:
1. Open file "sample.txt".
2. read line to buffer "string". (sunny has hardcoded data of string. If you remove that and add my algorithm, then you will have complete code).
3. Repeat step# 2 till EOF.

Hope you can convert these to code and make it work!

 

by: sanko50Posted on 2004-01-12 at 04:16:18ID: 10094712

thank you   sunnycoder  and   ssnkumar
    -----------------------------------

i have managed to implement the code usuing string only.
code
------
#include<stdio.h>

int main()

{
char last_char;
int count=0;
char string[] = "1 1 1 1 1 1 1 2 0 0";
char *temp = string;
last_char = *temp;

/* loop over input begin */

while(*temp != NULL)
{
while ( *temp == last_char )
{
     count++;
     temp = temp+2; //+2 for intervening space

}

if ( count > 2)
     /* print * last_char count*  */
     printf("*%c%d*",last_char,count);
else
     for(int i=0;i<count;i++)
     printf("%c",last_char);
      
last_char = *temp;
count = 0;

}
/* loop over input end*/
}

now i  am going to use FILE....as  ssnkumar pointed out i will pour the data first into buffer.
then  i have some question
--------------------------
question1 :  i have data around  int buffer[300000]. here  i usued  *temp != NULL for teminating the input loop.....but in case of buffer, how can i terminate????????

question2: i think i have to use temp=temp+4;  bcoz i have integer data , and in VC++ integer data takes 4 byte. am i right????

basically i will have my data stored in an integer buffer (not in a string)  so i have change a little bit??? is there any other change i have to do???

thanks

 

by: ozoPosted on 2004-01-12 at 06:03:11ID: 10095085

    temp = temp+2; //+2 for intervening space
take care that this does not put you beyond the end of  string

 

by: sanko50Posted on 2004-01-12 at 06:13:03ID: 10095134

"take care that this does not put you beyond the end of  string "

hi ozo ... but  above code running correctly....why r u saying so?? temp=temp+2; ...i am increasing the addresses.....so at last i will reach to a addresses whose content is NULL and loop will  terminate.....

and  infact  this code is runing and giving me the result.....

it was usuing a string so NULL was the terminating character.....can you suggest how can terminate  the loop if it is an  int buffer[]?????

 

 

by: sanko50Posted on 2004-01-12 at 06:42:45ID: 10095279


hi , i  have stored data in the buffer....and i want to  print like above
i am not getting successes , getting debug at the run time......simply i have changed char to integer... where is the mistake????

my code
-------------

#include<stdio.h>
int main()
{
   FILE *outfile = fopen("data.txt","w");
   int last_integer;
   int count =0;
   int buff[10]={1,1,1,1,1,1,-45,26,0,0};
   buff[11]='E';
   int* temp= buff;
   last_integer = *temp;
   
while(*temp != 'E')  // E is for termination

{
while ( *temp == last_integer )
{
     count++;
     temp = temp+4; // next integer

}

if ( count > 2)

{    
     printf("*%d%d*",last_integer,count);
     fprintf(outfile,"*%d%d*", last_integer,count); // writing in file also
}

     
else
      { for(int i=0;i<count;i++)
     printf("%d",last_integer);
     fprintf(outfile,"%d",last_integer);
      }
      
last_integer = *temp;
count = 0;
}

/* loop over input end*/
}
      
this code is giving runtime debug......



 

by: sanko50Posted on 2004-01-12 at 19:11:43ID: 10101128

hi...i have done it.....i changed
int buff[20]={1,1,1,1,1,1,-45,26,0,0}; buff[10]=-999;//for sentinel value
while(*temp != -999) // for termination
temp = temp+1// next integer...

although i am getting on the screen correct result.....but in my file last 0 is not coming....
output in the screen
----------------------
*16*452600

output in the file
------------------
*16*45260 // last 0 is absent!!!!!!!!!!

had it not been 0 as the last character in the buffer,everything goes fine....what happened to the file...why it has absorbed the last zero???
N.B : i opened file in the notepad and notepad


************************************************************
i want also one algorithm for decoding also....i.e given  *16*452600  this in the file i will expand this into 1,1,1,1,1,1,-45,26,0,0  into a buffer.....that is just the opposite thing .....  plz  can you give SOME STEPS TO start with???????????

many many thanks.......


 

by: ssnkumarPosted on 2004-01-12 at 20:17:08ID: 10101462

One small change to your code!
Put both printf() and fprintf() statements after the for loop inside braces.
Original code:
else
     { for(int i=0;i<count;i++)
     printf("%d",last_integer);
     fprintf(outfile,"%d",last_integer);
     }
After change:
     else
     {
        for(int i=0;i<count;i++)
        {
           printf("%d",last_integer);
           fprintf(outfile,"%d",last_integer);
        }
     }

One more suggestion: Follow indentation properly:-)

Steps for the decoding part:
1. Read from file to a character buffer.
2. Start scanning the buffer, char by char.
3. Repeat steps 4-8 for each scanned char.
4. If the current char is '*', then read the next char and store it in temp_char.
5. Read the next chars (and also keep count of these) till another '*' is found.
6. Convert them to integer count.
6. Display the temp_char count times.
8. If the test of step 3 fails, then display the current char.

Hope this will help....

-ssnkumar

 

by: sanko50Posted on 2004-01-13 at 11:29:29ID: 10107089


thank you for the reply ..yes i have corrected the first part....and trying to do the second part(decoding)....in fact i  have  thought about your algorithm....

one confusion is there in your  step 6 :  " Convert them to integer count "  in fact i am thinking for that........


say i have something like below....

*135*1234  you will read first ' 1'  '3'  '5'  then you will repeat 1  (10*3+5)=35 times....probabily you wanted to mean this by your step 6.


other confusion  is, say , instead of printing  the  output i want to store in the  INTEGER  buffer( i want to store ,not to
print ) so in that case if i read according to char by char then  1234  of   *135*1234 will be stored as
  buff[i]=1  buff[i+1]=2 buff[i+2] =3 buff[i+3]=4

but may be in the encoder i wanted to mean 1234 = 12  34  ( two different number)

natuarally i would expect in the decoder buffer like  buff[i] =12  and buff[i+1]=34 (which is not possible if i read char by char)

so meaning will be completely changed !!!!!!

if at the storing time i could not distinguish these  difference then my data processing will be wrong.

in fact priniting will not cause any problem but storing will create problem ...so i have to be cautious at the storing time. how can i store properly ?????

that is why i am not able solve till now...........

waiting for your reply for any suggestion

thanks

 

by: ssnkumarPosted on 2004-01-13 at 20:36:36ID: 10111023

1. Regarding confusion# 1:
>*135*1234  you will read first ' 1'  '3'  '5'  then you will repeat 1  (10*3+5)=35 times....probabily you wanted to mean this by your step 6.
What you have understood is correct and hope that's what your expectation is also.....!? If yes, Then there is no confusion:-))

2. Till now, I was thinking that, you have only single digit numbers!
    If you want to take care of digits with more than one digit, then a small addition/change to your algorithm.
    I will write the steps to be followed from step 8. (other steps remain the same)

8.   If the test of step 4 fails, THEN,
9.   BEGIN
10.     int temp_num = 0.
11.     read the next char into temp_char.
12.     while temp_char is not equal to space (' ') DO,
13.     BEGIN
14.         temp_num = (temp_num * 10) + (temp_char - '0').
15.         read the next char into temp_char.
16.     END (OF WHILE)
17. END (OF IF)

So, whatever is stored in temp_num after step 17 is your number.
Hope this works:-)

 

by: sanko50Posted on 2004-01-15 at 22:28:56ID: 10127496

hi, i am sorry still i could not make it......after putting efforts still i am getting wrong results.....i have been debugging a lot to find it the logical error and to fix it....but not been successful yet....

based upon your STEPS i wrote

code
---------

#include<stdio.h>
#include<stdlib.h>
int main()
{
 FILE *infile;
   
 int k, i=0;
 static int p=0;
 int MY_BUFFER[100]; // to get output
 char buff[100];  // to store from file
 
 char var,temp_char,read_char;
 
 infile = fopen("c:\\abc.txt","r");
 if(infile == NULL) printf(" file can not be opened");
   
read_char='A';   // any character to start
 
fscanf(infile,"%c",&var);

while(!feof(infile))
 {
 
  buff[i]=var;
 
  i=i+1;
  fscanf(infile,"%c",&var);   //
 
 }
 buff[i]='\0';  // sentinel to stop the loop below
    k=0; int count = 0;


  while(buff[k]!='\0')  // scanning the buffer
 {
       
  if(buff[k]=='*')

  {  
     
        
        temp_char=buff[k+1];
         
      int  temp_num1=0;
      int m =k+2;
        
      read_char=buff[m];


      while(read_char!='*')
      {  
       count=count+1;
       temp_num1 = (temp_num1*10)+(read_char-'0');
       read_char=buff[m+count];  
     
         //  k = k+1;
     
      }
     
        k =m+count;

        k=k+1;

     

        
      for(int N=0;N<temp_num1;N++)
      {  
       
      MY_BUFFER[p]=(temp_char-'0');
        
      p++;
     
      }  
 
 
  }  // end of if
 
 
  int temp_num2=0;
  temp_char=buff[k];  
  int count2=0;
 
  while(temp_char !=' ')   // untill a gap
  { count2=count2+1;  
   temp_num2 = (temp_num2*10)+(temp_char - '0');
   temp_char = buff[k+count2];
  }
   
 
  k=k+count2;

  if(temp_num2 !=0)

  {
  MY_BUFFER[p] = temp_num2;
  p++;
  }
 
k=k+1;  

 }   // end while
 


 printf("\n\n\n");

for(int j =0;j<p;j++)

printf("%d\t",MY_BUFFER[j] );


}  // end of main


my sample input(in the file) :
-----------------------------------
123 *124* 5 6 7 8 9  //( note the spaces)

output i am getting in the MY_BUFFER[j]
-----------------------------------------------

123     1       1       1       1       1       1       1       1       1
1       1       1       1       1       1       1       1       1       1
1       1       1       1       1       5       6       7       8       12362332
84      Press any key to continue


as you see i am missing 9 in MY_BUFFER[] !!!!!!!!!  and also some other garbages has come also!!!!!!!!

where have i gone wrong????????? how can i fix it???????

THANKS FOR THE HELP

 

by: ssnkumarPosted on 2004-01-16 at 00:34:39ID: 10127841

There is a small problem when you reach the last char!
The next char after the last char can be a '\0' or '\n'!! You have take care of this also. So change your while statement to:

      while ((temp_char != ' ') && (temp_char != '\0') && (temp_char != '\n'))  // untill a gap

With this change your code should work perfectly:-)

-ssnkumar      

 

by: sanko50Posted on 2004-01-16 at 02:34:58ID: 10128274

ok thanks....after correcting as per your suggestion...... it is working for THAT SAMPLE INPUT ONLY.


but  if give

sample input (from file)
----------------------------
123 *124* 5 6 *12* 7 8 9

and

sample input (from file)
----------------------------
123 *124* 5 6 7 8 9 *12*  

i  am not getting output at all!!!!!!!!
but my encoder may produce these type of output as well !!!!!....my decoder should be  GENERAL  so that it could handle  all type of output(from encoder) is not it????

so why my decoder fails to decode for these  above two  particular output???




N.B : i have usued 'a'  as a sentinel for my buff[]...is it creating any trouble??

thanks  



 

by: ssnkumarPosted on 2004-01-16 at 02:54:13ID: 10128360

One more small mistake! The "count" variable has to be initialized every time in the while loop. The code change is as below:

  while (buff[k] != '\0')       // scanning the buffer
  {
       count = 0;                 // I HAVE ADDED THIS LINE NOW
....

  }

I tested it for different input sequences. I think it must work without much problem now:-)

-ssnkumar

 

by: sanko50Posted on 2004-01-16 at 06:07:51ID: 10129492

THANK YOU FOR THE PROMPT,CLEAR,NICE,AND ELABORATED DESCRIPTION  

yes .....its working now for all sequences....

in fact i tested

with the file input
---------------------
123 *124* 5 6 *12* 7 8 9
---space--------
123 *124* 5 6 7 8 9 *12*  


and i have got the correct result  for this ALSO!!!.....its ok now....fine...

actually i am trying to  COMPRESS THE FILE SIZE  by searching the repeation of character and using  CODING.

i would have been more happier if  i could  REMOVE THE SPACES  in the encoder output.  and  still the decoder could  decode the code correctly...........bcoz in that case  some more bytes (i.e spaces) could have been compressed, and consequently i could get a more compressed file..........but  probabily  i feel it would be difficult for the decoder if it do not find the spaces , what is your opinion?????????

anyway, upto this is enough for me.......

thanks
 



 




 

by: ssnkumarPosted on 2004-01-20 at 22:16:14ID: 10161850

Try Huffman encoding. That's a very good compression algorithm.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...