943,469 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2868
  • C RSS
Dec 3rd, 2004
0

Help with UNIX more c code

Expand Post »
Ok, for a lab I have to write the UNIX more filter in C, but I have got everything done execpt one thing. I only hvae it to where it can read 20 lines at a time. But what I want to do is if argv[1] == -Pxx where xx is the amount of lines you want to display at a time. but I need to use a scanf() and a atoi command I think but I have not idea on how to do that. Here is my code and where it sets the amount of line sto be printed is in bold.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

main(int argc, char *argv[])

{
int line_no = 0;
int i=1;
FILE *fp;
char ch;
int linec;
char line[256];

while ( i < argc )
{
if ( (fp = fopen(argv[i],"r")) != NULL )
{
printf("\n%s\n",argv[i]);
while(!feof(fp) )
{
line_no = (++line_no) % 20;
if ( line_no == 0 )
{
printf("---Press enter for more lines---");
if ( (ch = getchar()) == 'q')
exit(0);
}

if ( fgets(line, 255, fp) != NULL ) /* not eof on line */
printf("%s", line);
}
fclose(fp);
}

++i;
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
jifiii is offline Offline
45 posts
since Nov 2004
Dec 4th, 2004
0

Re: Help with UNIX more c code

read the parameter, strip away the -P, and use atoi on the rest of the string to convert it into an integer number.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 4th, 2004
0

Re: Help with UNIX more c code

could you right a peice of code for me that would do that for me please.
Reputation Points: 10
Solved Threads: 1
Light Poster
jifiii is offline Offline
45 posts
since Nov 2004
Dec 5th, 2004
0

Re: Help with UNIX more c code

Quote originally posted by jifiii ...
could you right a peice of code for me that would do that for me please.

He could, (heck I probably could), but how would you learn anything? Just take his instructions, and implement them the best way that you can think of. Then post that, and see what, if anything, we can suggest.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Dec 5th, 2004
0

Re: Help with UNIX more c code

Ok, can you answer me this question how do you strip away the -P part.
Reputation Points: 10
Solved Threads: 1
Light Poster
jifiii is offline Offline
45 posts
since Nov 2004
Dec 5th, 2004
0

Re: Help with UNIX more c code

there's a function to take a substring of a longer string. Find out what it is and how you use it.
Any manual, helpfile or other reference will tell you what to do.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 5th, 2004
0

Re: Help with UNIX more c code

Where would I find that at, because I have the program form working except from inputing how many lines you want to display at a time.
Reputation Points: 10
Solved Threads: 1
Light Poster
jifiii is offline Offline
45 posts
since Nov 2004
Dec 6th, 2004
0

Re: Help with UNIX more c code

what did I say? Did you even bother to read past the first few words or are you too lazy even for that?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 6th, 2004
0

Re: Help with UNIX more c code

if you work on unix/linux, check the man page for getopt() function. It helps very much. At first it seems like an overhead, but in fact is very usefull.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
nvanevski is offline Offline
12 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: mode prog in c
Next Thread in C Forum Timeline: Draw sin curve w/ user input, how to code sin()?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC