LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-22-2005, 06:34 AM   #1
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Rep: Reputation: 0
Help In C files


I have to create a ranges out of these lines in file

Input:
------------
9849201348
9849201349
9849201350
9849201400
9849203000
9849209000
9849210000
9849215000
9849220000

output
-------
range 1: 9849201348 to 9849210000

range 2: 9849215000 to 9849220000

Usually there will be millions of lines of code .The numbers will be in sorted order but not in a consecutive manner...like after 9849201350 ,9849201400 may appear...The difference between

9849201400 -9849201350 =50 .If the difference between 2 numbers is greater than 10000 then we have to take the numbers encountered as one range.

i,e range 1: 9849201348 to 9849210000 for the above example...

If i follow the rule like taking the first line in the series and adding 10000 and taking it as one range ..But the number may not be present in the file.so shall i have to go linear way or what...Can anyone please help me out in finding a solution
 
Old 01-22-2005, 09:45 AM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
This reeks of homework.

Further, it reeks of elementary C homework. If I am wrong then please explain the real world situation where you need to parse the output in this manner... but I know I am not wrong.
 
Old 01-22-2005, 09:50 AM   #3
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Most code is a series of yes and no answered questions.

Discard the computer for a while and get a very big piece of paper and a pencil.

Make a flow chart of if-then questions and see where that takes you.

Back to the computer and write functions to perform each if-then question.
You/ve done a large protion of this in your post already.
 
Old 01-22-2005, 09:57 AM   #4
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
Help in C files

I need to parse a huge file which contain the telecom subscriber data and prepare the ranges of the subscriber data . This is the real world example...
 
Old 01-22-2005, 10:15 AM   #5
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
Suppose if i have to read two lines in a loop then how should i do that

for example below r 2 lines and how should i get the values and store them...

9849201348
9849201349


fp=fopen("input.txt","r");

if (fp==NULL)
{
printf ("\n File does not exist....");
exit(0);
}

while (fgets(ln,100,fp)!=NULL)

{

/* Here i get only one line ..... *///


how should i store 2 values and compare that ...

}
pls help me
 
Old 01-22-2005, 11:10 AM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
As frob23 states

Quote:
This reeks of homework.
You are basically saying:

"I know how to open a file for reading"

"I know how to check that it worked"

"I dont know how to create a couple of variables and compare their contents"

No one is going to believe this isn't homework!
 
Old 01-22-2005, 11:35 AM   #7
tamoneya
Member
 
Registered: Jan 2005
Location: MA
Distribution: Ubuntu 7.10
Posts: 558

Rep: Reputation: 30
I would try to put all of the values in an array and then try to sort them with a fast sorting algorithm. The you can move down the array with a loop and compare values.
 
Old 01-22-2005, 11:44 AM   #8
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
Code:
(defconstant +RANGE+ 10000)
(defvar *count* 0)

(defun printrange (x y z)
        (setq *count* (1+ *count*))
        (format t "Range ~a: ~a to ~a~%" *count* x y)
        (values z))

(defun findrange (start current)
        (setq a (read *standard-input* nil 'EOF))
        (if (eq a 'EOF)
                (printrange start current a)
                (if (< +RANGE+ (- a start))
                        (printrange start current a)
                        (findrange start a))))

(setq b (read *standard-input* nil 'EOF))
(if (eq b 'EOF)
        (print "ERROR: Empty File."))
(loop
(setq c (read *standard-input* nil 'EOF))
        (if (eq c 'EOF)         ; Last value is unmatched not part
                (return))       ; of a range. End program.
        (setq b (findrange b c))
        (if (eq b 'EOF)
                (return)))
I was bored... here is the working code... you wanted it in LISP right?

EDIT: Actually, I would love the datafile your instructor gave you... so I could run it with more than the test values and ensure I have it correct. It does produce the output for the input data you gave.

Code:
clisp range.lsp < datafile
Range 1: 9849201348 to 9849210000
Range 2: 9849215000 to 9849220000


EDIT #2: Before anyone points it out... yes the recursion is pointless... although it is one way to solve the problem... I was just playing with LISP today and decided to do it recursive style... besides... that's how LISP likes it.

Last edited by frob23; 01-22-2005 at 12:04 PM.
 
Old 01-22-2005, 11:56 AM   #9
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
Quote:
Originally posted by tamoneya
I would try to put all of the values in an array and then try to sort them with a fast sorting algorithm. The you can move down the array with a loop and compare values.
The values are already sorted... this is a toy problem from a programming class. It expects a nearly perfect input. The values are not incremental but they are in sequence.
 
Old 01-23-2005, 12:02 AM   #10
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
I want it in C language .please help me
 
Old 01-23-2005, 12:26 AM   #11
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
Quote:
The Rules
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
http://www.linuxquestions.org/rules.php

None of us were born yesterday. Many of us have had dedicated courses on programming. Almost all of us can smell a homework problem from a mile away. Please, do it yourself. You learn more by doing that and we would be doing you a grave disservice if we just gave you the answer.

If it is truly not a homework assignment... use clisp (installed on many Linux distributions and available off the web) and run the program given above. If it was not a homework problem then implementation detail would be minor.

Last edited by frob23; 01-23-2005 at 12:27 AM.
 
Old 01-23-2005, 12:49 AM   #12
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
hai

hai frobe its not a homework problem or so...I was trying to solve it ...Really i tried to solve the problem but could not able to do so.thats the reason i'm asking you the help ....As you have already done it so ...thats the reason im asking you so....right now i failed to solve the solution
 
Old 01-23-2005, 01:03 AM   #13
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
Quote:
The Rules
The best place for your question is in the forum. We get a large number of emails. If your email contains a technical question we will kindly point you to the forums.
Please do not email your questions. If you have a very specific question regarding this issue, fine someone will help. But we are not going to do it for you. The LISP code was posted above only because it amused me and not as an indication of the help you should expect. No one here is going to give you a complete working example.

I recommend you read the following. I didn't write it but it might help you.

http://home.att.net/~jackklein/ctips01.html
 
Old 01-23-2005, 01:12 AM   #14
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
Code:
int main()   /* Do NOT make main void... it returns an int */
/* It is horrible coding style to expect the compiler to not barf on that */
{
    FILE *fp;
    static char ch;
    char ln[100],
    fp=fopen("input.txt","r");
    if (fp==NULL)
   {
        printf ("\n File does not exist....");
        exit(0);
   }

   while (fgets(ln,100,fp)!=NULL)
   {
    /* How  i read 2 lines and store the values */
    /* Well, you have one line already (stored in ln)... why not */
    /* Read another one?  Maybe into a new variable this time? */


    strcpy(ch,ln);
    }

    return 0;
}
 
Old 01-23-2005, 01:20 AM   #15
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
is it the same way .....as i wrote below...

int main() /* Do NOT make main void... it returns an int */
/* It is horrible coding style to expect the compiler to not barf on that */
{
FILE *fp;
char ch;
char ln[100];
char str[100];
fp=fopen("input.txt","r");
if (fp==NULL)
{
printf ("\n File does not exist....");
exit(0);
}

while (fgets(ln,100,fp)!=NULL)
{
/* How i read 2 lines and store the values */
/* Well, you have one line already (stored in ln)... why not */
/* Read another one? Maybe into a new variable this time? */

strcpy(ch,ln)

fgets(str,100,fp)!=NULL
strcpy(ch1,str);

atoi(ch1)-atoi(ch) <10000
}

return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:38 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration