Solved

Last Word(Count)

Posted on 1998-12-01
7
254 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

821 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