Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:23:19+00:00 2026-06-12T13:23:19+00:00

I am trying to make a small program that takes in input from a

  • 0

I am trying to make a small program that takes in input from a file that is delimited (by “|”)
for example one line in the file looks like this:

name|phone|address|city|state|zip

For some reason everytime I try to get it to write everything that is in the array of structures to a binary file the linking of the array goes wrong. I have been looking at this for a few hours and cannot figure out why it does this. One of the problems I have noticed is if I decide to run the program and I print out what is in index 0 of the array after writing something to index 1 it messes up index 0 (each index holds a struct). I am not sure why.

Please help, I have been banging my head on the keyboard for hours trying to figure out why it gives me this problem. Sorry, forgot to mention that it should be run with the first parameter being the path to the file ex
./ExerciseOne.out /path/to/file.txt

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


/*
 * ExerciseOne.c
 *
 *  Created on: Sep 24, 2012
 *      Author: kevin
 */

typedef struct personal_Info {
    char *Name;
    char *Phone;
    char *address;
    char *city;
    char *state;
    int zip;

} PInfo ;

void loopTokens(PInfo *, char *);
void PrintStruct(PInfo *);
void PrintStructArray(PInfo p[], int);
void transferText(PInfo *, int);
void transferTextBin(PInfo p[], int);
void readTextBin(FILE *, PInfo *, int );

int main(int argc, char *argv[]){
    int index = 0;
    int numLines = 0;
    char *lineOfText = malloc(80);
    //Open File
    FILE *fp;
    if ((fp = fopen(argv[1], "r")) == NULL){
    printf("File could not be opened");
    }
    else {
        fclose(fp);
    fp = fopen(argv[1], "r");//inputs the argv as the vile to read
    }
    fscanf(fp, "%d \n", &numLines);


    //Create Arrays for Binary and Text structs
    PInfo TextInfo[numLines];
    PInfo *TextInfo_ptr;



/*Commented code
 * TextInfo[index] = *TextInfo_ptr;//Point to structure ##Unneeded##
 *  printf("this is before the transferTextBin %s \n", TextInfo[0].Phone);//Works
 */
    while(!feof(fp)&&index<numLines){
      TextInfo_ptr = &TextInfo[index];//Structure points to the proper hole in array; current index//Looks like it advances the count for the ptr
      fgets(lineOfText, 80, fp);
      loopTokens(TextInfo_ptr,  lineOfText);//puts pointer to the array index into function
      printf("this is before the transferTextBin %s \n", TextInfo_ptr->Phone);//Works
      printf("this is before the transferTextBin %s \n", TextInfo[0].Phone);//problem second time around
      printf("this is before the transferTextBin %s \n", TextInfo[1].Phone);//works
      //PrintStruct(TextInfo_ptr);
      index++;
    }


    //transferText(TextInfo, numLines);//Will do it in regular

    printf("this is before the transferTextBin %s \n", TextInfo[0].Phone);//Here it is suddenly losing it
    transferTextBin(TextInfo, numLines);//will do it in bin

    fclose(fp);
    FILE *fBIN;
    fBIN = fopen("fOUT.dat", "rb");// may need to insert this into method
    //readTextBin(fBIN, BinInfo, numLines);
    return(0);
}


void loopTokens(PInfo *p, char *textLine){//Want to pass in INDIVDUAL structs
    char *buffer;
    int index = 0;

  p->Name = malloc(80);
  p->Phone = malloc(80);
  p->address = malloc(80);
  p->city = malloc(80);
  p->state = malloc(80);
  p->zip = 0;

        /* mallocate each field in the current structure */

    buffer = strtok(textLine, "|"); /* Tokenize the string */

    while(buffer != NULL && strcmp(textLine,"\n")){ /* loop through all tokens */
            if(index == 0){
            //strcpy(p->Name,textLine);
            p->Name = buffer;
            buffer = strtok(NULL, "|");
            index++;
            }
            else if(index == 1){
            p->Phone = buffer;
            buffer = strtok(NULL, "|");
            index++;
            }
            else if(index == 2){
            p->address = buffer;
            buffer = strtok(NULL, "|");
            index++;
            }
            else if(index == 3){
            p->city = buffer;
            buffer = strtok(NULL, "|");
            index++;
            }
            else if(index == 4){
            p->state = buffer;
            buffer = strtok(NULL, "|");
            index++;
            }
            else if(index == 5){
                p->zip = 0;
            p->zip = (int)atoi(buffer);
            buffer = strtok(NULL, "|");
            index++;
            }
  }
  index = 0;

}

    void PrintStruct(PInfo *p){//Gets pointer to the struct
        printf("This is the supposed name: %s The Phone is : %s, the address is : %s %s %s %d\n", p->Name, p->Phone, p->address, p->city, p->state, p->zip );
    }
    void PrintStructArray(PInfo p[], int lines){//Gets Entire array of structs; prints everything out
  int index = 0;
    while(index < lines){
      printf("This is the supposed name: %s The Phone is : %s, the address is : %s %s %s %d\n", p[index].Name, p[index].Phone, p[index].address, p[index].city, p[index].state, p[index].zip );
    }
  }
void transferText(PInfo *p, int numLines){//Transfers text
  FILE *fOUT;//open file for writing
  int index = 0;
        fOUT = fopen("fOUT.txt", "w");//Open file for writing

        while(index < numLines){
          fputs(p->Name,fOUT);
          fputs(p->Phone,fOUT);
          fputs(p->address,fOUT);
          fputs(p->city,fOUT);
          fputs(p->state,fOUT);
          fputs(p->zip,fOUT);
        }

        fclose(fOUT);

    }
void transferTextBin(PInfo p[], int numLines){
    FILE *fOUT;//open file for writing
    //int index = 0;
    fOUT = fopen("fOUT.dat", "wb");
    printf("this is the phone of the first index %s", p[0].Phone );
    //printf("Ti the supposed name: %s The Phone is : %s, the address is : %s %s %s %d\n", p[index].Name, p[index].Phone, p[index].address, p[index].city, p[index].state, p[index].zip );
    //while(index < numLines){
/*  fwrite(p[index]->Name, sizeof(p[index]->Name), 1, fOUT);
    fwrite(p[index]->Phone, sizeof(p[index]->Phone), 1, fOUT);
    fwrite(p[index]->address, sizeof(p[index]->address), 1, fOUT);
    fwrite(p[index]->city, sizeof(p[index]->city), 1, fOUT);
    fwrite(p[index]->state, sizeof(p[index]->state), 1, fOUT);*/
    //fwrite(p->zip, sizeof(p->zip), 1, fOUT);
//  }
    fclose(fOUT);
    }







void readTextBin(FILE *fIN, PInfo *bInfo, int numLines){
    int index = 0;
    //char *buffer;
    printf("This is what is in the File: ");
    //bInfo[index].Name = fread(buffer, 80, 1, fIN);
    //bInfo[0]->Name = malloc(80);
    while(!feof(fIN) && index<numLines){
        fread(bInfo[index].Name, 80, 1, fIN);
        fread(bInfo[index].Phone, 80, 1, fIN);
        fread(bInfo[index].address, 80, 1, fIN);
        fread(bInfo[index].city, 80, 1, fIN);
        fread(bInfo[index].state, 80, 1, fIN);
        //fread(bInfo[index]->zip, 80, 1, fIN);
        PrintStruct(bInfo);
        index++;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:23:20+00:00Added an answer on June 12, 2026 at 1:23 pm

    In loopTokens, you ought to strcpy the values from the textLine buffer to the char* members. You malloc memory for name, address etc.,

    void loopTokens(PInfo *p, char *textLine){
        char *buffer;
        int index = 0;
    
        p->Name = malloc(80);
        /* snip */
        buffer = strtok(textLine, "|"); /* Tokenize the string */
    
        while(buffer != NULL && strcmp(textLine,"\n")){ /* loop through all tokens */
                if(index == 0){
                //strcpy(p->Name,textLine);
                p->Name = buffer;
                buffer = strtok(NULL, "|");
                index++;
                }
    

    but when you assign e.g. p->Name = buffer;, you lose the reference to the malloced memory (bad), and let p->Name point to a char in the array pointed to by textLine. strtok modifies the buffer it is invoked on, and returns (NULL or) a pointer into that buffer.

    So p->Name and friends all point into the textLine buffer, whose contents changes when you read in the next line.

    Instead of p->Name = buffer;, you should

    strcpy(p->Name, buffer);
    

    (you have allocated no less memory to p->Name than to textLine, so there’s no danger of writing outside the allocated memory, provided you check whether malloc returned NULL).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a small program that will show a video from a
I'm trying to make a small program that could intercept the open process of
I am trying to make a small program that fills the username and password
I am trying to make a simple program that will subtract an object from
I'm trying to make a small Python script that is executed by clicking an
I'm trying to make a small program more robust and I need some help
I would like to write a small piece of program that launches threads, consumes
I'm trying to make a GUI for a small program I wrote with the
I am making a small program that reads a text file and stores it
I am trying to make a small drawing program for small children. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.