problem: Reorganize structures using array of pointers and I/O redirec

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4409 Views - Last Post: 29 May 2012 - 02:49 PM Rate Topic: -----

#1 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

problem: Reorganize structures using array of pointers and I/O redirec

Posted 22 May 2012 - 01:31 PM

I have to code a C program to receive structures from a file such as:

Mr Smith
123 fake ave (exactly like this)
city, state
zip

and then output them to another file in order by zip code.
I have to use an array of pointers to structures. (I'm assuming to pass structure info to functions? though I don't know why exactly it has to be an array..)
I also have to use I/O redirection for input/output to files.

The way I was thinking of going about programming this was to first go through the input file, pull and copy all the zip codes into an array, sort the array from least to greatest, and finally output each structure according to the sorted array.

My main concern is, this method would require me to read the input file multiple times and I don't know how to go about doing that using I/O redirection. Also, I don't know how to read each line in C without using getline.


I hope I didn't make this sound too confusing...I'm not exactly sure my method is ideal. Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: problem: Reorganize structures using array of pointers and I/O redirec

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 22 May 2012 - 02:10 PM

When doing input/output redirection you don't actually open any files, you input data like you are using the console, and output data to the console. When you call the file you "redirect" stdin and stdout to a file. See this link:Standard Input and Output Redirection.

Quote

My main concern is, this method would require me to read the input file multiple times and I don't know how to go about doing that using I/O redirection. Also, I don't know how to read each line in C without using getline.

Think loops.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 22 May 2012 - 02:51 PM

View Postjimblumberg, on 22 May 2012 - 02:10 PM, said:

When doing input/output redirection you don't actually open any files, you input data like you are using the console, and output data to the console. When you call the file you "redirect" stdin and stdout to a file. See this link:Standard Input and Output Redirection.

Quote

My main concern is, this method would require me to read the input file multiple times and I don't know how to go about doing that using I/O redirection. Also, I don't know how to read each line in C without using getline.

Think loops.

Jim



I've never done I/O redirection before. Say I wanted to read from the beginning of the file before reaching the end. How would I go about doing that?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 22 May 2012 - 05:39 PM

Quote

Say I wanted to read from the beginning of the file before reaching the end. How would I go about doing that?

Think loops!

First get your program working, getting input from the user via the console. With redirection the input file replaces your user's input from the console.

Jim

This post has been edited by jimblumberg: 22 May 2012 - 05:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 22 May 2012 - 09:16 PM

1. Start with a small test program to read a few data sets (from stdin/keyboard) into an array of struct ... and display the array ... to screen

2. Learn how to make a dynamic array of struct and/or an expandable dynamic array of struct

3. Learn how to sort that array of struct on some field in the struct ... Maybe using C's qsort and passing in your compare function pointer to qsort

4. Then just write that sorted array to stdout (the screen)

5. Now recode your program (to eliminate any prompts for input and any extra headings for output) so that the executable file takes, from the command line, a file name for input ... and a file name for output using redirection as below

This DIC snippet may give you a 'running start' ...

http://www.dreamincode.net/code/snippet5142.htm


Now to run the executable from the command line or a batch file ...
yourSortStruct.exe < fileIn.txt > fileOut.txt

where the input is redirected by '<' from stdin (usually the keyboard) to take input from file fileIn.txt

and instead of printing to screen. (the usual stdout) ... the running program prints to the redirected output file ... by '>' fileOut.txt

This post has been edited by David W: 22 May 2012 - 10:07 PM

Was This Post Helpful? 1
  • +
  • -

#6 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 01:37 AM

View PostDavid W, on 22 May 2012 - 09:16 PM, said:

2. Learn how to make a dynamic array of struct and/or an expandable dynamic array of struct


for the most part, this got me up and starting. But I'm not allowed to use an array of structs.
Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 05:05 AM

View Postalzy101, on 22 May 2012 - 04:31 PM, said:

I have to code a C program to receive structures from a file such as:

Mr Smith
123 fake ave (exactly like this)
city, state
zip

and then output them to another file in order by zip code.
I have to use an array of pointers to structures. (I'm assuming to pass structure info to functions? though I don't know why exactly it has to be an array..)
I also have to use I/O redirection for input/output to files.
...


Ok ... Just create new memory space for each struct as you read them from the file ... and store those pointers in a (growing) array of pointers to struct ...

And what do you think you will have? Your desired array of pointers to each new struct.

Now sort that array of pointers based on the zips in each struct that each pointer to struct points to ...

You could use fgets, and a large buffer, to read each of the four lines... into each C string in your struct.

If you want to use dynamic C strings, you could also use a function like this:
char* newCopy( const char* sourceCstr );

Don't forget to free each dynamic C string in each struct when you are done with the array of pointers to struct ( if you use dynamic C strings )

Then to free each memory space for each of your new struct's ...

Then to free, if you used a dynamic array, the memory to hold the array of pointers to struct

This post has been edited by David W: 29 May 2012 - 06:27 AM

Was This Post Helpful? 0
  • +
  • -

#8 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 01:03 PM

View PostDavid W, on 29 May 2012 - 05:05 AM, said:

View Postalzy101, on 22 May 2012 - 04:31 PM, said:

I have to code a C program to receive structures from a file such as:

Mr Smith
123 fake ave (exactly like this)
city, state
zip

and then output them to another file in order by zip code.
I have to use an array of pointers to structures. (I'm assuming to pass structure info to functions? though I don't know why exactly it has to be an array..)
I also have to use I/O redirection for input/output to files.
...


Ok ... Just create new memory space for each struct as you read them from the file ... and store those pointers in a (growing) array of pointers to struct ...


Sorry, this concept confuses me a bit. Perhaps it is best to show you what I have so far and you can tell me what it is I am doing wrong. (hopefully it's not the whole code)

Here's the relevant part of the code:

#include <stdio.h>
#include <stdlib.h>

    #include "lab7head.h"

int main (void)
{
    struct input *data = malloc(50 * sizeof(*data));

    readzip(data);

    return 0;
}



There's the main for testing input... (readzip())
I looked all over the internet, and the few books I have for the memory allocation of struct. But I'm not exactly sure if I did it right. In my understanding it's an array of pointer structs? which I don't think I can use???? (I was told specifically not to use an array of structs. Is this the same thing?)

and here's the relevant part of the header file:

struct input
{
    char *pername, // lastname , firstname
         *stradr,  // street address
         *citstat; // city and state

    int zip;       // zip code

    char *blank;
};

void readzip(struct input pdata[]) //reads all zip codes and places them in array to be sorted.
{
    short zipcount = 0;
    int ziparray[50];

    for (zipcount; !feof(stdin); zipcount++)
    {
        gets(&pdata[zipcount].pername);
        gets(&pdata[zipcount].stradr);
        gets(&pdata[zipcount].citstat);
        scanf("%d", &pdata[zipcount].zip);
        gets(&pdata[zipcount].blank);
    }
    zipcount--;
    printf("%s", &pdata[0].pername);
}



The problem I'm getting with reading is that when I output each element of the struct, it's all scrambled up.

eg: the input file reads:

Smith John
12345 fake st
City State
56789

but the second pername element in the array reads something like "Smth123 Cit56789" It doesn't make sense...

This post has been edited by Oler1s: 29 May 2012 - 01:29 PM
Reason for edit:: It's [/code] not [\code]

Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 01:33 PM

A number of problems abound in your code.

> for (zipcount; !feof(stdin); zipcount++)

Your ending condition is nonsensical. Think about what feof does, think about what you are doing in your code.

> gets(

No gets. Ever. Use fgets.

> &pdata[zipcount].pername

What is that & doing there?

Also, pername is a pointer. Where does it point?
Was This Post Helpful? 1
  • +
  • -

#10 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 02:02 PM

View PostOler1s, on 29 May 2012 - 01:33 PM, said:

A number of problems abound in your code.

> for (zipcount; !feof(stdin); zipcount++)

Your ending condition is nonsensical. Think about what feof does, think about what you are doing in your code.

> gets(

No gets. Ever. Use fgets.

> &pdata[zipcount].pername

What is that & doing there?

Also, pername is a pointer. Where does it point?


how do you suggest I fix the !feof(stdin)?

I changed gets to fgets and that fixed the issue of the program reading the information all weird. Thank you!

The & is there because the structure itself is a pointer. That's the only way the program would write the data to the variable without crashing. Pointers are my weakest point in programming...
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 02:04 PM

Pleas post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 02:12 PM

View Postjimblumberg, on 29 May 2012 - 02:04 PM, said:

Pleas post your current code.

Jim


header:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct input
{
    char pername[20], // lastname , firstname
         stradr[20],  // street address
         citstat[20]; // city and state

    int zip;       // zip code

    char blank[20];
};

void readzip(struct input pdata[]) //reads all zip codes and places them in array to be sorted.
{
    short zipcount = 0;
    int ziparray[50];

    for (zipcount; !feof(stdin); zipcount++)
    {
        fgets(&pdata[zipcount].pername,20,stdin);
        fgets(&pdata[zipcount].stradr,20,stdin);
        fgets(&pdata[zipcount].citstat,20,stdin);
        scanf("%d", &pdata[zipcount].zip);
        fgets(&pdata[zipcount].blank,20,stdin);
    }
    zipcount--;
    printf("%s", &pdata[1].citstat); //used to test each array element in struct
}

void ouput() //ouputs structures to output file. //not used yet
{

}





main:
#include <stdio.h>
#include <stdlib.h>

    #include "lab7head.h"

int main (void)
{
    struct input *data = malloc(50 * sizeof(*data));

    readzip(data);

    return 0;
}



none of the elements past pdata[0] work...

(eg:
pdata[0].citstat displays the information properly
pdata[1] and beyond don't work {it seems to read the wrong line...}
)
Was This Post Helpful? 0
  • +
  • -

#13 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 02:21 PM

I think the problem with the displaced input is that scanf() doesn't clear the line. So the subsequent fgets reads the blank space or '\n' character at the end of the zip code line throwing off the rest of the program from reading everything properly. Is there an alternative method to reading the zip code line into an int apart from scanf()?
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 02:22 PM

What is the file name of your "header" file? If it is lab7head.h then you should rethink that. Header files should not contain actual implementations.

Next that file should be giving you several errors/warnings:

Quote

main.c||In function ‘readzip’:|
main.c|21|warning: statement with no effect|
main.c|23|error: passing argument 1 of ‘fgets’ from incompatible pointer type|
/usr/include/stdio.h|624|note: expected ‘char * restrict’ but argument is of type ‘char (*)[20]’|
main.c|24|error: passing argument 1 of ‘fgets’ from incompatible pointer type|
/usr/include/stdio.h|624|note: expected ‘char * restrict’ but argument is of type ‘char (*)[20]’|
main.c|25|error: passing argument 1 of ‘fgets’ from incompatible pointer type|
/usr/include/stdio.h|624|note: expected ‘char * restrict’ but argument is of type ‘char (*)[20]’|
main.c|27|error: passing argument 1 of ‘fgets’ from incompatible pointer type|
/usr/include/stdio.h|624|note: expected ‘char * restrict’ but argument is of type ‘char (*)[20]’|
main.c|30|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’|
main.c|19|warning: unused variable ‘ziparray’|
main.c|33|warning: no previous declaration for ‘ouput’|

When using fgets() you need to pass the address of your variable, and with a C-string you pass the address by using the name of the variable:

char test[100];
fgets(test, 100, stdin);



Again a header file should not contain any function implementations.

Jim
Was This Post Helpful? 1
  • +
  • -

#15 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

Re: problem: Reorganize structures using array of pointers and I/O redirec

Posted 29 May 2012 - 02:24 PM

also my scanf() call doesn't read the zipcode properly at all. instead of, say: '67095' it gives me a much longer random set of numbers.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2