Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #99681

Re: [File Handling] fread(), fseek() and text file

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: [File Handling] fread(), fseek() and text file
Date 2016-12-16 18:40 +0000
Organization Fix this later
Message-ID <o31cc7$4gs$1@dont-email.me> (permalink)
References (6 earlier) <703f5537-45b1-4946-af97-3615c15dbbd3@googlegroups.com> <o316fv$dr5$1@dont-email.me> <f2a424b3-1a56-4c49-b792-ed9fcd9969e9@googlegroups.com> <o318om$m6s$1@dont-email.me> <bb0da670-4df4-4db4-a023-bac9f68b039d@googlegroups.com>

Show all headers | View raw


On 16/12/16 18:03, Dexterr wrote:
> 	// Memory allocation for struct Components
> 	Components *series_connection = (Components*) malloc(sizeof *series_connection);

For the last time: you don't need the cast.

Components *series_connection = malloc(sizeof *series_connection);

(If you can't learn, I can't teach you.)

> - now I have to do 3 functions, but problem is how to split from
 > this function 3 more functions, because all three parts are in
 > the while loop.

int linenumber = 0;

while(fgets(line, sizeof line, fp) != NULL)
{
   ++linenumber;

   if(is_comment(line) == 0)
   {
     if(tokenise(series_connection, line)) == NUM_FIELDS)
     {
       output(series_connection);
     }
     else
     {
       fprintf(stderr,
               "Input error on line %d.\n",
               linenumber);
     }
   }
}

That just leaves you to write is_comment(), tokenise() and output().

Here's a freebie: is_comment():

int is_comment(const char *line)
{
   return line[0] == '*';
}

> - > one more think, for instance the variable components must have limit for 5 characters.
> I have put it as a pointer in struct. If I turn it in array - char component[5], I have problem
> in function to add a values from file.
>
> I mean how to reffer pointer to pointer from struct ?
>
> For instance how to do it here ?
>
> series_connection->component = strtok(line, " \t\n");

if(strlen(series_connection->component) > MAX_COMPONENT_LENGTH)
{
   reject the line
}

If you would rather that series_connection contained arrays rather than 
pointers, that's fine. You can set up a pointer like this:

char *token = strtok(line, " \t\n");
if(token != NULL)
{
   if(strlen(token) >= sizeof series_connection->component)
   {
     report an error and reject the line
   }
   else
   {
     strcpy(series_connection->component, token);
   }
}

This gives you even more reason to put all this sort of thing in a 
separate function.

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


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