Solved

Last Word(Count)

Posted on 1998-12-01
7
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

623 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