LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2005, 01:26 AM   #16
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48

strcpy(ch,ln)

ch is not a string, it is also undefined. Dike that line out.

strcpy(ch1,str);

ch1 is not even included in the file, further why copy the line? Dike that line out.

atoi(ch1)-atoi(ch) <10000

This might be good if you had a test around it... and possibly switched the < to a >.

Last edited by frob23; 01-23-2005 at 01:33 AM.
 
Old 01-23-2005, 04:46 AM   #17
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Don't forget you are trying to compare to numerical values!

Dump fgets and replace it with fscanf

You can then put the input directly into an integer variable

/* Define your Integers */

int v_first, v_this, v_last, v_count;

/* Initialise counter */
v_count=0;

In your while loop:

while (fscanf(fp,"%d",v_this))
{
/* now have a number stored as an integer in v_this */
if (v_count == 0)
v_first = v_this;
v_count++;

if( put test criteria here )
{
/* begining of new sequence */
printf("Sequence: %d to %d\n", v_first, v_last);
v_first = v_this;
}

v_last = v_this;
}

Now it's all done bar the typing, and the understanding ofcourse.

Seriously, get your self some good reference material, this isn't what the forums are for.
 
Old 01-23-2005, 06:48 AM   #18
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Btw, how should be values be saved?

The maximum of an "unsigned long integer" is 4294967296 & these phone nos. are beyond that ?!
 
Old 01-23-2005, 07:11 AM   #19
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
the nos are stored as double... if im not wrong....

and there are some cases to consider .... if the diff between the first and the immediate second line is greater than 10000 then we have to store the range as shown first one .If the diff between 3 or more lines is greater then we have to print the range from the number to previous one like range 3 .. and if it encounters end then we have to print only range.... like range4:

9849201348
9849212348
9849223348
9849225348
9849227348
9849242348

then

range 1 : 9849201348
range 2 : 9849211348
range 3 : 9849223348 to 9849227348
range 4 : 9849242348
range 4 : 9849242348


i'm not able to write the cases. Pleas help me out
 
Old 01-23-2005, 08:46 AM   #20
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
Please help me out i'm not able to get the logic .... this is what i wrote...

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
FILE *fp,*fo;
char ch[100];
char ln[100];
char str[100];
char string[100];
char * pEnd;
double v_count=0,v_first;
clrscr();
fp=fopen("c:\\TC\\SAMPLE\\file.txt","r");
if (fp==NULL)
{
printf ("\n File does not exist....");
exit(0);
}
while (fgets(ln,100,fp)!=NULL)
{
if(v_count==0)
{
v_first=strtod(ln,&pEnd);
}
v_count++;

if(fgets(str,100,fp)!=NULL)
{
if ((strtod(str,&pEnd)-v_first)>10000.00
&&(strtod(str,&pEnd)-strtod(ln,&pEnd)>10000.00))
{
fo=fopen("c:\\tc\\sample\\output.txt","w");
fprintf(fp,"range=%s \n",v_first);
v_first=strtod(str,&pEnd);
fclose(fo);
}

else if((strtod(str,&pEnd)-strtod(ln,&pEnd)>10000))
{
fo=fopen("c:\\tc\\sample\\output.txt","w");
printf("\n Its Here ");
fprintf(fp,"range=%s&&%s\n",v_first,ln);
v_first=strtod(str,&pEnd);
fclose(fo);
}
}
else
{
fo=fopen("c:\\tc\\sample\\output.txt","w");
fprintf(fp,"range=%s \n",ln);
printf("\n Finished");
fclose(fo);
}
}

return 0;
}
 
Old 01-24-2005, 01:21 PM   #21
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
I'm not convinced I should tell you the correct answer, however ....

My comments should help you understand why I've written it this way:
Code:
#include <stdio.h>
#include <stdlib.h>
#define RANGE_LIMIT 10000

/*
 *  Program to list ranged numbers from an input file.
 *  
 *  Comments
 *    1. For each entry in v_infile, we need to check that it is not larger than the previous by more than the RANGE_LIMIT.
 *    2. Output is formatted without decimal places (same as input) although it is stored in a double variable.
 *    3. Input is obtained using fgets, which returns a string, so needs casting to a double.
 *    4. Comments added through out the code, to make it easier to read!
 */
int main()
{
   FILE *v_infile, *v_outfile;
   char v_first_s[100], v_this_s[100];
   double v_first, v_this, v_last;

   v_infile=fopen("data","r");

   if (v_infile)
   {
      /*
       * If I managed to open the file for reading,
       * grab the first line into string: v_first_s
       * and check it was not an empty file
       */
      if(fgets(v_first_s,sizeof(v_first_s),v_infile) == NULL)
      {
         printf("No data in file!\n");
         fclose(v_infile);
         return(3);
      }

      /*
       * Input file was not empty so open the output file for writing!
       */
      v_outfile=fopen("output","w");

      if (v_outfile)
      {
        /*
	 * If I can write to the output file,
	 * convert the string: v_first_s to a double: v_first
	 * then copy the double: v_first to another double: v_last
	 */
         v_first = (double)atof(v_first_s);
         v_last = v_first;

         while (fgets(v_this_s,sizeof(v_this_s),v_infile)!=NULL)
         {
	    /*
	     * For each entry in the input file,
	     * grab the line into string: v_this_s
	     * and convert it to a double: v_this
	     */
	    v_this = (double)atof(v_this_s);
            if((v_this - v_first) > RANGE_LIMIT)
            {
	       /*
	        * if the value of double: v_this
		* is greater than the value of double: v_first
		* by more than RANGE_LIMIT
		* Add a line to the output file and
		* start a new range.
		*/
               fprintf(v_outfile, "Range: %10.0f to %10.0f\n", v_first, v_last);
               v_first = v_this;
            }
	    /*
	     * End of this loop change the value of double: v_last
	     */
            v_last = v_this;
         }

	 /*
	  * Following line catches the last piece of data
	  * and passes it to the output file!
	  */
         fprintf(v_outfile, "Range: %10.0f to %10.0f\n", v_first, v_last);

	 /*
	  * Finished writing to output file, close it!
	  */
	 fclose(v_outfile);
      } else {
         /*
	  * Could not open output file for writing!
	  * Print error message and close program.
	  */
         printf("Unable to write to output file!\n");
	 return(4);
      }

      /*
       * Finished reading from input file, close it!
       */
      fclose(v_infile);
   } else {
      /*
       * Could not open input file for reading!
       * Print error message and close program.
       */
      printf("Unable to open input file!\n");
      return(2);
   }
   
   /*
    * Program completed without any errors!
    */
   return(0);
}
When writing any programming language, it's important to comment your code.
You may understand it when you write it, but it may not be you that needs to modify it.
Also you may need to understand someone else's code, and it's a lot easier if it's got lot's of comments in it!
If your not on a programming course, you may want to think about going on one.
C is not the easiest language to teach yourself.
 
Old 01-24-2005, 09:29 PM   #22
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
Thanks
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Able to write files to MP3 player but no files written into actual device? olnex Linux - Hardware 0 11-11-2005 06:32 AM
How program to read files with extension .dat y .cfg files COMTRADE in fedora 1? ivonne Linux - Software 0 11-22-2004 11:42 AM
How can konqueror view html files in .gz & .bz2 files directly? ailinzhe Linux - Software 5 05-24-2004 08:36 AM
xine plays only (a part of) the sound of a wmv, some divx files and crushes mp4 files bezoomny Linux - Software 1 03-10-2004 07:33 PM
How to filter files in files and files which are in a sub-directory with "grep"? Piero Linux - Newbie 9 08-29-2003 02:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration