So I have this project to program an "auto parts management" C program. Basically I need to be able to add and delete lines of text from a text file. Also, I need to be able to edit lines of text. Lines of text are in the format:

PART NAME : OWNER : STATUS : SYSTEM DATE : CO NAME

Although I don't know much about them, structures would seem like they would be useful. I think in my case it would look like this:

struct parts
{
  char pname [PNAME_LEN+1];
  char owner [OWNER_LEN+1];
  char status [3];                                //toggles between in/out
  char date [10];                                // MM/DD/YYYY format.
  char oname [ONAME_LEN+1];
} part1, part2, part3;                         //etc... I assume I need a loop
                                                //to increment part.

To add a part, the program will prompt the user for a pname and an owner. So once I have those two things captured in variables, I need to initialize part1 so it contains "pname:owner:in" with the date and co name left blank. Will this work?

struct parts part1;
strcpy(part1.pname, pname);
strcpy(part1.oname, owner);
strcpy(part1.status, "in");

How would I then print part1 into a text file?

So I have this project to program an "auto parts management" C program. Basically I need to be able to add and delete lines of text from a text file. Also, I need to be able to edit lines of text. Lines of text are in the format:

PART NAME : OWNER : STATUS : SYSTEM DATE : CO NAME

Although I don't know much about them, structures would seem like they would be useful. I think in my case it would look like this:

struct parts
{
  char pname [PNAME_LEN+1];
  char owner [OWNER_LEN+1];
  char status [3];                                //toggles between in/out
  char date [10];                                // MM/DD/YYYY format.
  char oname [ONAME_LEN+1];
} part1, part2, part3;                         //etc... I assume I need a loop
                                                //to increment part.

To add a part, the program will prompt the user for a pname and an owner. So once I have those two things captured in variables, I need to initialize part1 so it contains "pname:owner:in" with the date and co name left blank. Will this work?

struct parts part1;
strcpy(part1.pname, pname);
strcpy(part1.oname, owner);
strcpy(part1.status, "in");

How would I then print part1 into a text file?

I would use fwrite

fwrite(&part1, sizeof(struct parts), 1, fd);

Where fd = FILE *stream

Edited 7 Years Ago by gerard4143: n/a

Can anyone find my mistake(s)? I am sure there are a few. I am not sure if scanf("%c", part1.pname) is even legal. I am getting an error when trying to compile. "In function main, two or more data types in declaration specifiers" on line 16 and "warning: initialization from incompatible pointer type" on line 28.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main(void)
{
    
    struct parts {
      char pname [20];
      char owner [20];
      char status [3];                       //toggles between in/out
      char date [10];                       // MM/DD/YYYY format.
      char oname [20]; 
  } 
    
    struct parts part1;                 //LINE 16, although I think it is
                                                 //caused somewhere else.
    
    printf("Part name?");
    scanf("%c", part1.pname);

    printf("Owner?");
    scanf("%c", part1.oname);

    strcpy(part1.status, "in");

    FILE *fp;
    size_t count;
    const char *str = &part1;                    //LINE 28
 
    fp = fopen("parts.txt", "w");              
    if(fp == NULL) {
        perror("failed to open parts.txt");
        return EXIT_FAILURE;
    }
    
    fwrite(str, sizeof(struct parts), 1, fp);
    return EXIT_SUCCESS;
}

Edited 7 Years Ago by Mattpd: n/a

I am not sure if scanf("%c", part1.pname) is even legal

Then why would you put it in your program?

#
const char *str = &part1; //LINE 28

You have const char *str and your assigning the address of struct parts to it without a cast..

Edited 7 Years Ago by gerard4143: n/a

Then why would you put it in your program?

#
const char *str = &part1; //LINE 28

You have const char *str and your assigning the address of struct parts to it without a cast..

I put it in so people can understand what I am trying to do, and if it's wrong, hopefully they can suggest the right way.

I am not too familiar with cast. I need to change the variable type of part1 before this initialization? Or is this initialization wrong to begin with?

This article has been dead for over six months. Start a new discussion instead.