Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: [File Handling] fread(), fseek() and text file |
| Date | 2016-12-16 14:02 +0000 |
| Organization | Fix this later |
| Message-ID | <o30s2a$88l$1@dont-email.me> (permalink) |
| References | <395a3e8e-f06d-4f7d-a2f5-76d334b9d50a@googlegroups.com> <o30dqj$np1$1@dont-email.me> <1d89377e-acd5-47d7-abca-1a3d28b65678@googlegroups.com> |
On 16/12/16 13:28, Dexterr wrote:
>
>
>> It's a text file, right? So why are you reading text descriptions of
>> numbers into numeric fields? The results will be meaningless.
>>
>> Use fgets() to retrieve the line:
>>
>> char line[130] = ""; /* your limit of 128,
>> + 1 for newline,
>> + 1 for '\0' */
>>
>> while(fgets(line, sizeof line, pFile) != NULL)
>> {
>> if(line[0] != '*')
>> {
>>
>
> Mr Richard, thanks for your response. I am very grateful on your help.
> I will send my solution and biger answer but I must ask you about this solution for comment.
>
> For instance in the text file above we can have more comments. Comments can be only in one line but that means we can have one comment in two or three lines and in front of each
> there is '*' sign.
>
> for instance
>
> * This is comment
> * This is second line of the same comment
> * and this is the last line of the whole comment
> R1 0 1 (100/1)
> C0 1 2 (1/1000000)
> L1 2 3 (47/1000)
>
> --
> So, if we have:
>
> char line[130] = ""; /* your limit of 128,
>> + 1 for newline,
>> + 1 for '\0' */
>>
>> while(fgets(line, sizeof line, pFile) != NULL)
>> {
>> if(line[0] != '*') ->>>> this part is the problem
No, it isn't. It's the solution to your comment problem.
>> {
>>
>
> - >> if(line[0] != '*') - suppose that we have the comment only in the first line.
If only the first line is a comment, only the first line will be ignored.
> What to do when we have more lines of comments and each begin with '*' ?
Each line that starts with a '*' will be ignored.
> This make a confusion and have no idea how to solve it.
There shouldn't be any confusion. Each line is processed separately. For
every line, the first question you ask is: "is this line a comment?" You
do that by inspecting the first character of the line, line[0]. If it's
a '*' character, that line is a comment, so you ignore it and go round
the loop again. Only if it is *not* a comment do you continue to process
that line.
Then, when that line has been processed, you go round the loop again.
And again, the first thing you do is test to see if the line is a
comment line, by again inspecting line[0].
Try this program out, and see for yourself. It is *not* a solution to
your original requirement --- you made it clear that you intend to write
that yourself --- but it will *illustrate* my point here.
#include <stdio.h>
int main(void)
{
char line[130] = "";
while(fgets(line, sizeof line, stdin) != NULL)
{
if(line[0] != '*') /* that is, if this line is NOT a comment line */
{
fputs(line, stdout);
}
}
return 0;
}
Input:
* This is a comment, which will be ignored.
* So is this.
This line is not a comment, and must be printed.
This line isn't a comment either.
* But don't display this one.
This one is okay to display.
* Not this one.
This is the last line.
Processing:
./comments < test.in
Output:
This line is not a comment, and must be printed.
This line isn't a comment either.
This one is okay to display.
This is the last line.
And that's exactly what I suggested you should do, and that is, I think,
exactly what you need. I really don't see the problem.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 01:10 -0800
Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 09:59 +0000
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 05:28 -0800
Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 14:02 +0000
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 06:30 -0800
Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 15:20 +0000
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 08:54 -0800
Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 17:00 +0000
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 09:32 -0800
Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 17:38 +0000
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 10:03 -0800
Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 18:40 +0000
Re: [File Handling] fread(), fseek() and text file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 20:16 +0000
Re: [File Handling] fread(), fseek() and text file Keith Thompson <kst-u@mib.org> - 2016-12-16 12:46 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 05:47 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 05:49 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 05:43 -0800
Re: [File Handling] fread(), fseek() and text file Barry Schwarz <schwarzb@dqel.com> - 2016-12-17 08:48 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 12:03 -0800
Re: [File Handling] fread(), fseek() and text file Keith Thompson <kst-u@mib.org> - 2016-12-17 12:22 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 12:23 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 12:26 -0800
Re: [File Handling] fread(), fseek() and text file asetofsymbols@gmail.com - 2016-12-18 12:41 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-18 13:04 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-18 13:08 -0800
Re: [File Handling] fread(), fseek() and text file asetofsymbols@gmail.com - 2016-12-19 06:14 -0800
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-19 10:38 -0800
Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-19 18:51 +0000
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-19 11:14 -0800
Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-19 19:35 +0000
Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-19 10:43 -0800
csiph-web