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 15:20 +0000 |
| Organization | Fix this later |
| Message-ID | <o310kr$o5b$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> <o30s2a$88l$1@dont-email.me> <027c95a8-183b-4d34-8214-cdc41be578b6@googlegroups.com> |
On 16/12/16 14:30, Dexterr wrote:
> Mr Richard, sorry I am reading faster then I think. ;) Your solution is great. It was my mistake.
> It was misunderstanding. You did exactly what is asked. Comments to avoid with if statement
> (line[0] != '*') is what was looking for last 7 days. ;)
>
> I didn't think that I can do it with strtok() function. But, this part is essential.
>
> I will explain what is my problem now.
>
> This is my new code (but not complete and will explain what is my problem now) for this part of my exercise:
>
> ---
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define FILENAME "netlist.txt"
>
> typedef struct
> {
> char component[5];
> unsigned a_node;
> unsigned b_node;
> char value[25]; // vidi kasnije da li ovo da prebacim u pointer ???
> } Components;
>
> typedef struct
> {
> char comp_new[5];
> unsigned first_node;
> unsigned second_node;
> char val_new[25];
>
> }SortComponents;
>
> char line[130];
>
> int main(void)
> {
> FILE *pFile = fopen(FILENAME, "r");
> Components *series_connection = (Components*) malloc(siezeof(*series_connection));
If you write it like this, *AS I SUGGESTED LAST TIME*:
Components *series_connection = malloc(sizeof *series_connection);
then not only is it a lot easier to type, but you're also more likely to
spot when you've misspelled sizeof as siezeof.
And you're still not checking whether the call succeeded.
Not that it matters, since you're not using it. (And indeed you don't
need it.)
Components series_connection = {0};
will give you a nice, normal struct to work with. But, as it turns out,
you don't need it.
> Now, my program must read text file and that text file
> put in the following form on the screen. I mean my
> program MUST print out on the screen next content
>
> 0 - R1(100/1) - 1
> 1 - C0(1/1000000) - 2
> 2 - L1(47/1000) - 3
>
> ---
>
> What that means. During reading input text file, the program must make a sorting.
All you have to do is move some fields around a little.
R1 0 1 (100/1)
C0 1 2 (1/1000000)
L1 2 3 (47/1000)
has to become
0 - R1(100/1) - 1
1 - C0(1/1000000) - 2
2 - L1(47/1000) - 3
So you get your input line:
"R1 0 1 (100/1)"
Tokenise on spaces, keeping everything as a string:
"R1" "0" "1" "(100/1)"
Let's call these tokens t[0], t[1], t[2], t[3].
Now just output them like this:
printf("%s - %s%s - %s\n", t[1], t[0], t[3], t[2]);
and you're done.
> - > My question what is the smartest way to make
> this sort with strtok() function, I mean how to
> initialise a values from reading file (from
> Components) to a new structure SortComponent ?
The strtok function is for tokenising (which you *do* need to do), not
for sorting (which you don't). And you don't need either of your structs.
The following program should get you started:
#include <stdio.h>
#include <string.h>
#define FILENAME "netlist.txt"
#define NUM_FIELDS 4
int main(void)
{
char *component = NULL;
char *a_node = NULL;
char *b_node = NULL;
char *value = NULL;
char line[130] = "";
FILE *fp = fopen(FILENAME, "r");
if(fp != NULL)
{
while(fgets(line, sizeof line, fp) != NULL)
{
if(line[0] != '*')
{
component = strtok(line, " \t\n");
a_node = strtok(NULL, " \t\n");
b_node = strtok(NULL, " \t\n");
value = strtok(NULL, " \t\n");
if(component != NULL &&
a_node != NULL &&
b_node != NULL &&
value != NULL)
{
printf("%s - %s%s - %s\n",
b_node, a_node, value, component);
}
}
}
fclose(fp);
}
else
{
fprintf(stderr, "Can't find %s.\n", FILENAME);
}
return 0;
}
Input data:
$ cat netlist.txt
* This is a comment
R1 0 1 (100/1)
C0 1 2 (1/1000000)
L1 2 3 (47/1000)
Process:
$ ./components
Output:
1 - 0(100/1) - R1
2 - 1(1/1000000) - C0
3 - 2(47/1000) - L1
--
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