Solved

Last Word(Count)

Posted on 1998-12-01
7
260 Views
Last Modified: 2010-04-15
I was going over some of my answers I received in the past couple of weeks and thinking about the question I submitted 11/24.  Could this program be modified as to show line numbers of a file being passed thru it so it could be used for as a quick reference, as to what line numbe each string is on.

Hopefully this is the last time I beat on this program, unless I get another brainstorm.

 
0
Comment
Question by:tester090797
7 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1254763
How so you want to handle words that occur on more than one line?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1254764
You can read the file a line at a time (instead of a character at a time) by using fgets() and incrementing a line-number counter.  Then loop on the charecters of the line one by one using something like: for (p = line; *p; ++p) { /* ... */ }
0
 

Author Comment

by:tester090797
ID: 1254765
What I was thinking about was showing outputing the file showing not only the file but the line number for each string.  Something like this:

1 #include <stdio.h>
2 main()
3 {
etc.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Expert Comment

by:kavitag
ID: 1254766
Tester,
I guess you can use Alexo's answer excluding looping the characters of the line.
use fgets to get a line and use a counter that is incremented when you fget

something like this
int p = 0;
char buf[1024];
while (!eof(File))
{
p++;
fgets(buf,sizeof(buf),File);
printf("%d %s\n",p,buf);
}
0
 

Author Comment

by:tester090797
ID: 1254767
I really appreciate the help I been receiving.  Unlike doing a crosswork puzzle and being stuck on a clue, you can always wait until the next day's newspaper.  This last piece of code confuses me, do I gut out my old program to insert this or do I just insert it after the character loop.
0
 

Accepted Solution

by:
kavitag earned 220 total points
ID: 1254768
The following is test.c

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

main()
{
int p=0;
char buf[1024];
FILE *f;

f = fopen("test.c","r");
while (fgets(buf, sizeof(buf), f)) {
      printf("%d %s",++p,buf);      
}
}

Output of test.c

1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 main()
6 {
7 int p=0;
8 char buf[1024];
9 FILE *f;
10
11 f = fopen("test.c","r");
12 while (fgets(buf, sizeof(buf), f)) {
13       printf("%d %s",++p,buf);      
14 }
15 }

0
 
LVL 16

Expert Comment

by:imladris
ID: 1254769
I think people are unsure as to which code you are using. Did you adopt rwilsons code? scrapdogs? Is Q.10101927 the right previous question to be looking at?

0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C: GetDiskFreeSpaceEx() for default drive 14 114
C hashtable library 3 100
Windows Updates failing due to Diskpart not configured correctly 8 130
Raspberry Pi 3 to send text message 9 151
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question