Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Last Word(Count)

Posted on 1998-12-01
7
Medium Priority
?
289 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 880 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

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.

Question has a verified solution.

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

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…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

916 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