Solved

Last Word(Count)

Posted on 1998-12-01
7
235 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
Comment Utility
How so you want to handle words that occur on more than one line?
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:kavitag
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now