C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 21st, 2012, 03:52 PM
slemma slemma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 slemma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 8 sec
Reputation Power: 0
Load_data to memory

Hi Guys,

I am new to C programing. I have the following assignment and what tried. It is not working any tip is appreciated.

Load_data: This function is automatically called when the program is started. It reads owner and property information from files. If the files do not exist, notify the user, create the files and display the main menu. Once a file is open, data is read into an array of structs by dynamically creating sufficient memory for each entry read from the file.

Define a struct to represent the information for an owner, containing the following fields:

• Number (int)
• Name (char[])
• E-mail address (char[])
• Phone number (char[])

Define a struct to represent the information for a property, containing the following fields:

• Owner (char[])
• Zip code (int)
• Property number (int)
• Bedrooms (int)
• Rental amount (float)

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

struct {
int number;
char name[36];
char email[20];
char phone[10];
} Owner;

struct {
char owner_name[36];
char zipcode[12];
char phone_number[20];
int bedrooms;
float rental_amount;
} Property;

int main(void)
{
/* declare a file pointer */
FILE *infile1,*infile2;
char *buffer;
long numbytes;



/* open an existing file for reading */
infile1 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\owner.txt", "r");
infile2 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\property.txt", "r");

/* quit if the file does not exist */
if(infile1&&infile2 == NULL)
return 1;

/* Get the number of bytes */
fseek(infile1&&infile2, 0L, SEEK_END);
numbytes = ftell(infile1&&infile2);

/* reset the file position indicator to
the beginning of the file */
fseek(infile1&&infile2, 0L, SEEK_SET);

/* grab sufficient memory for the
buffer to hold the text */
buffer = (char*)calloc(numbytes, sizeof(char));

/* memory error */
if(buffer == NULL)
return 1;

/* copy all the text into the buffer */
fread(buffer, sizeof(char), numbytes, infile1&&infile2);
fclose(infile1&&infile2);

/* confirm we have read the file by
outputing it to the console */
printf("The file called test.dat contains this text\n\n%s", buffer);

/* free the memory we used for the buffer */
free(buffer);

return 0;
}

Reply With Quote
  #2  
Old October 21st, 2012, 04:04 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
Check this line out

Code:
#define REC_SIZE = 10


Also, please use code tags when you post code.

Note I tried compiling your program and recieved a few errors and warnings.

The errors
Code:
test.c: In function ‘load_data’:
test.c:57:14: warning: assignment makes integer from pointer without a cast [enabled by default]
test.c:58:12: error: incompatible types when assigning to type ‘char[36]’ from type ‘char *’
test.c:59:13: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
test.c:60:13: error: incompatible types when assigning to type ‘char[10]’ from type ‘char *’
test.c:62:8: error: ‘index’ undeclared (first use in this function)
test.c:62:8: note: each undeclared identifier is reported only once for each function it appears in
test.c:42:7: warning: unused variable ‘token’ [-Wunused-variable]
test.c:40:8: warning: unused variable ‘j’ [-Wunused-variable]
test.c:40:5: warning: unused variable ‘i’ [-Wunused-variable]
test.c:39:6: warning: unused variable ‘buffer’ [-Wunused-variable]
test.c: In function ‘main’:
test.c:79:1: warning: ISO C90 forbids mixed declarations and code [-pedantic]
test.c:80:16: error: ‘index’ undeclared (first use in this function)
test.c:83:1: error: expected declaration or statement at end of input
test.c:74:21: warning: unused variable ‘propertyFilePtr’ [-Wunused-variable]
test.c:74:7: warning: variable ‘ownerFilePtr’ set but not used [-Wunused-but-set-variable]
test.c:73:7: warning: unused variable ‘property_filename’ [-Wunused-variable]
test.c:83:1: warning: control reaches end of non-void function [-Wreturn-type]
[gerard@localhost test]$ clear
[gerard@localhost test]$ man strtok
[gerard@localhost test]$ gcc test.c -Wall -ansi -pedantic -o test
test.c: In function ‘load_data’:
test.c:57:14: warning: assignment makes integer from pointer without a cast [enabled by default]
test.c:58:12: error: incompatible types when assigning to type ‘char[36]’ from type ‘char *’
test.c:59:13: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
test.c:60:13: error: incompatible types when assigning to type ‘char[10]’ from type ‘char *’
test.c:62:8: error: ‘index’ undeclared (first use in this function)
test.c:62:8: note: each undeclared identifier is reported only once for each function it appears in
test.c:42:7: warning: unused variable ‘token’ [-Wunused-variable]
test.c:40:8: warning: unused variable ‘j’ [-Wunused-variable]
test.c:40:5: warning: unused variable ‘i’ [-Wunused-variable]
test.c:39:6: warning: unused variable ‘buffer’ [-Wunused-variable]
test.c: In function ‘main’:
test.c:79:1: warning: ISO C90 forbids mixed declarations and code [-pedantic]
test.c:80:16: error: ‘index’ undeclared (first use in this function)
test.c:83:1: error: expected declaration or statement at end of input
test.c:74:21: warning: unused variable ‘propertyFilePtr’ [-Wunused-variable]
test.c:74:7: warning: variable ‘ownerFilePtr’ set but not used [-Wunused-but-set-variable]
test.c:73:7: warning: unused variable ‘property_filename’ [-Wunused-variable]
test.c:83:1: warning: control reaches end of non-void function [-Wreturn-type]


How can you write code and have so many errors and warnings? When I write code I compile it with every change or addition.

Reply With Quote
  #3  
Old October 21st, 2012, 05:45 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 5 m 8 sec
Reputation Power: 1949
Quote:
Originally Posted by slemma
It is not working any tip is appreciated.

Tip #1: Instead of just simply saying "It is not working", tell us what that is supposed to mean. Won't compile? Won't run? Ignores input? Gives wrong output? Get output with minor glitches line leaving out a single space? All of that and infinitely more is encompassed by "It is not working", so you really do need to be much more specific.

Tip #2: Use code tags!

Heeding both tips will take you very far here.

Reply With Quote
  #4  
Old October 21st, 2012, 06:06 PM
slemma slemma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 slemma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 8 sec
Reputation Power: 0
I am sorry i loaded a wrong code. I have edit it.

Code:

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

 struct {
    int number;
    char name[36];
    char email[20];
    char phone[10];
} Owner;

 struct {
    char owner_name[36];
    char zipcode[12];
    char phone_number[20];
    int bedrooms;
    float rental_amount;
} Property;

int main(void)
{
/* declare a file pointer */
FILE    *infile1,*infile2;
char    *buffer;
long    numbytes;



/* open an existing file for reading */
infile1 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\owner.txt", "r");
infile2 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\property.txt", "r");

/* quit if the file does not exist */
if(infile1&&infile2 == NULL)
    return 1;

/* Get the number of bytes */
fseek(infile1&&infile2, 0L, SEEK_END);
numbytes = ftell(infile1&&infile2);

/* reset the file position indicator to
the beginning of the file */
fseek(infile1&&infile2, 0L, SEEK_SET);

/* grab sufficient memory for the
buffer to hold the text */
buffer = (char*)calloc(numbytes, sizeof(char));

/* memory error */
if(buffer == NULL)
    return 1;

/* copy all the text into the buffer */
fread(buffer, sizeof(char), numbytes, infile1&&infile2);
fclose(infile1&&infile2);

/* confirm we have read the file by
outputing it to the console */
printf("The file called test.dat contains this text\n\n%s", buffer);

/* free the memory we used for the buffer */
free(buffer);

return 0;
}


Quote:
Originally Posted by G4143
Check this line out

Code:
#define REC_SIZE = 10


Also, please use code tags when you post code.

Note I tried compiling your program and recieved a few errors and warnings.

The errors
Code:
test.c: In function ‘load_data’:
test.c:57:14: warning: assignment makes integer from pointer without a cast [enabled by default]
test.c:58:12: error: incompatible types when assigning to type ‘char[36]’ from type ‘char *’
test.c:59:13: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
test.c:60:13: error: incompatible types when assigning to type ‘char[10]’ from type ‘char *’
test.c:62:8: error: ‘index’ undeclared (first use in this function)
test.c:62:8: note: each undeclared identifier is reported only once for each function it appears in
test.c:42:7: warning: unused variable ‘token’ [-Wunused-variable]
test.c:40:8: warning: unused variable ‘j’ [-Wunused-variable]
test.c:40:5: warning: unused variable ‘i’ [-Wunused-variable]
test.c:39:6: warning: unused variable ‘buffer’ [-Wunused-variable]
test.c: In function ‘main’:
test.c:79:1: warning: ISO C90 forbids mixed declarations and code [-pedantic]
test.c:80:16: error: ‘index’ undeclared (first use in this function)
test.c:83:1: error: expected declaration or statement at end of input
test.c:74:21: warning: unused variable ‘propertyFilePtr’ [-Wunused-variable]
test.c:74:7: warning: variable ‘ownerFilePtr’ set but not used [-Wunused-but-set-variable]
test.c:73:7: warning: unused variable ‘property_filename’ [-Wunused-variable]
test.c:83:1: warning: control reaches end of non-void function [-Wreturn-type]
[gerard@localhost test]$ clear
[gerard@localhost test]$ man strtok
[gerard@localhost test]$ gcc test.c -Wall -ansi -pedantic -o test
test.c: In function ‘load_data’:
test.c:57:14: warning: assignment makes integer from pointer without a cast [enabled by default]
test.c:58:12: error: incompatible types when assigning to type ‘char[36]’ from type ‘char *’
test.c:59:13: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
test.c:60:13: error: incompatible types when assigning to type ‘char[10]’ from type ‘char *’
test.c:62:8: error: ‘index’ undeclared (first use in this function)
test.c:62:8: note: each undeclared identifier is reported only once for each function it appears in
test.c:42:7: warning: unused variable ‘token’ [-Wunused-variable]
test.c:40:8: warning: unused variable ‘j’ [-Wunused-variable]
test.c:40:5: warning: unused variable ‘i’ [-Wunused-variable]
test.c:39:6: warning: unused variable ‘buffer’ [-Wunused-variable]
test.c: In function ‘main’:
test.c:79:1: warning: ISO C90 forbids mixed declarations and code [-pedantic]
test.c:80:16: error: ‘index’ undeclared (first use in this function)
test.c:83:1: error: expected declaration or statement at end of input
test.c:74:21: warning: unused variable ‘propertyFilePtr’ [-Wunused-variable]
test.c:74:7: warning: variable ‘ownerFilePtr’ set but not used [-Wunused-but-set-variable]
test.c:73:7: warning: unused variable ‘property_filename’ [-Wunused-variable]
test.c:83:1: warning: control reaches end of non-void function [-Wreturn-type]


How can you write code and have so many errors and warnings? When I write code I compile it with every change or addition.

Reply With Quote
  #5  
Old October 21st, 2012, 06:31 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
Nice ideas, but this is not how c works. The expression
a && b
produces 0 or 1. It does not mean "do this function to both items".

Handling both files together is a good idea, to the extent that it's possible. You could make an array of FILE*infiles[2] and loop over that array. Without that array, using infile1 and infile2 of your program, you'll need to write more code treating the files separately. For example:

Code:
  /* open an existing file for reading */
  infile1 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\owner.txt", "r");
  if(NULL == infile1)		/* quit if the file does not exist */
    return 1;
  infile2 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\property.txt", "r");
  if(NULL == infile2) {		/* quit if the other file does not exist */
    fclose(infile1);		/* this file was opened, close it */
    return 2;
  }

  /* Get the number of bytes in the owner file*/
  fseek(infile1, 0L, SEEK_END);
  numbytes_in_owner = ftell(infile1);
  fseek(infile1, 0L, SEEK_SET);
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #6  
Old October 21st, 2012, 08:13 PM
slemma slemma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 slemma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 8 sec
Reputation Power: 0
Thank you for your help but i am still getting unorganized output. Here is the requirement: Load_function- is automatically called when the program is started. It reads owner and property information from files. If the files do not exist, notify the user, create the files and display the main menu. Once a file is open, data is read into an array of structs by dynamically creating sufficient memory for each entry read from the file.

Define a struct to represent the information for an owner, containing the following fields:

• Number (int)
• Name (char[])
• E-mail address (char[])
• Phone number (char[])

Define a struct to represent the information for a property, containing the following fields:

• Owner (char[])
• Zip code (int)
• Property number (int)
• Bedrooms (int)
• Rental amount (float)

Quote:
Originally Posted by b49P23TIvg
Nice ideas, but this is not how c works. The expression
a && b
produces 0 or 1. It does not mean "do this function to both items".

Handling both files together is a good idea, to the extent that it's possible. You could make an array of FILE*infiles[2] and loop over that array. Without that array, using infile1 and infile2 of your program, you'll need to write more code treating the files separately. For example:

Code:
  /* open an existing file for reading */
  infile1 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\owner.txt", "r");
  if(NULL == infile1)		/* quit if the file does not exist */
    return 1;
  infile2 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\property.txt", "r");
  if(NULL == infile2) {		/* quit if the other file does not exist */
    fclose(infile1);		/* this file was opened, close it */
    return 2;
  }

  /* Get the number of bytes in the owner file*/
  fseek(infile1, 0L, SEEK_END);
  numbytes_in_owner = ftell(infile1);
  fseek(infile1, 0L, SEEK_SET);

Reply With Quote
  #7  
Old October 21st, 2012, 08:48 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
You know that some of your data is not text...

Code:
struct {
    int number;/*not text*/
    char name[36];
    char email[20];
    char phone[10];
} Owner;

 struct {
    char owner_name[36];
    char zipcode[12];
    char phone_number[20];
    int bedrooms;/*not text*/
    float rental_amount;/*not text*/
} Property;


So this may not(will not) display those noted fields properly.

Code:
printf("The file called test.dat contains this text\n\n%s", buffer);

Reply With Quote
  #8  
Old October 21st, 2012, 08:54 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
You will fail this assignment until you understand that

infile1&&infile2 == NULL

means

infile1 && (infile2 == NULL)

true only when infile1 opened successfully and infile2 did not open.


ftell(infile1&&infile2)

ftell needs a pointer to a FILE type. You have given ftell either a 0 or a 1.

Reply With Quote
  #9  
Old October 22nd, 2012, 04:40 PM
slemma slemma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 slemma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 8 sec
Reputation Power: 0
Thanks for your help. I got it to work but now i am trying to right it in to an array of structs but output is not what i expected. I do not know what i am doing wrong. Below is the code: Any tip is appreciated.

Code:

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

 struct {
    int number;
    char name[36];
    char email[20];
    char phone[10];
} owner;

 struct {
    char owner_name[36];
    int zipcode;
    int number;
    int bedrooms;
    float rental_amount;
} property;

int main(void)
{
/* declare a file pointer */
FILE    *infile1,*infile2;
char    *buffer;
long    numbytes;
long    numbytes_in_owner;
long    numbytes_in_property;
int i;
char name[100];




/* open an existing file for reading */
infile1 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\owner.txt", "r");
if(NULL == infile1)
return 1;
infile2 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\property.txt", "r");
if(NULL == infile2) {		/* quit if the other file does not exist */
    fclose(infile1);		/* this file was opened, close it */
    return 2;
  }

/* quit if the file does not exist */
//if(infile1&&infile2 == NULL)
    //return 1;

/* Get the number of bytes */
fseek(infile1, 0L, SEEK_END);
numbytes_in_owner = ftell(infile1);
fseek(infile1, 0L, SEEK_SET);

fseek(infile2, 0L, SEEK_END);
numbytes_in_property = ftell(infile2);
fseek(infile2, 0L, SEEK_SET);

/* reset the file position indicator to
the beginning of the file */
fseek(infile1, 0L, SEEK_SET);
fseek(infile2, 0L, SEEK_SET);

/* grab sufficient memory for the
buffer to hold the text */
buffer = (char*)calloc(numbytes, sizeof(char));
//buffer = malloc(numbytes * sizeof(char));

/* memory error */
if(buffer == NULL)
    return 1;

/* copy all the text into the buffer */
fread(buffer, sizeof(char), numbytes, infile1);
fread(buffer, sizeof(char), numbytes, infile2);
fclose(infile1);
fclose(infile2);



fscanf(infile1, "%s,%d,%s,%d", &owner.number, owner.name,owner.email,&owner.phone);
    fclose(infile1);
          printf("owner number:%s - name:%d - email:%s - phone:%d",&owner.number, owner.name,owner.email,&owner.phone);

fscanf(infile2, "%s,%d,%d,%d,%2f", &property.owner_name, property.zipcode,property.number,property.bedrooms,&property.rental_amount);
    fclose(infile1);
          printf("Property owner:%s - name:%d - email:%s - phone:%d",&property.owner_name, property.zipcode,property.number,property.bedrooms,&property.rental_amount);



/* confirm we have read the file by
outputing it to the console */
printf("The files are \n\n%s", buffer);

/* free the memory we used for the buffer */
free(buffer);

int choice, shift;

Label1:

    printf  ("\t************************************");
    printf("\n\t*        MENU                      *");


           //Label1

    printf("\n\t*   1. add owner                   *");
    printf("\n\t*   2. add property                *");
    printf("\n\t*   3. View property info          *");
    printf("\n\t*   4. sort owner info             *");
    printf("\n\t*   5. view property info by rent  *");
    printf("\n\t*   6. sort property info          *");
    printf("\n\t*   7. sort owner info by pr number*");
    printf("\n\t*   8. view pr info by bedroom     *");
    printf("\n\t*   9. exit                        *");
    printf("\n\t************************************");


    printf("\n Please type your choice: ");
    scanf ("%d", &choice);
    //return option;


   // printf("\n Please enter 5 integers:");
   // scanf ("%d %d %d %d %d", &one, &two, &three, &four, &five);


    switch(choice)
        {
//add owner
case 1:
Label2:

printf("\n enter the owner info \n");
printf("\n enter the owner number =");
scanf("%d",&owner.number);
printf("\n enter the owner name =");
scanf("%d",owner.name);
printf("\n enter the Email=");
scanf("%s",owner.email);
printf("\n enter the owners phone=");
scanf("%s",&owner.phone);
infile1=fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\owner.txt","a+");
fprintf(infile1,"\n%d\t%s\t%s\t%d\t",owner.number, owner.name,owner.email,owner.phone);
fclose(infile1);


       printf("Owner record has been added successfully...\n");
       printf("\n\n1 -> Wish to add another record");
       printf("\n2 -> Wish to move to Main Menu");
       printf("\n3 -> Exit from Program\n");
       scanf("%d",&shift);
       if(shift==1)
        goto Label2;
       if(shift==2)
        goto Label1;
       if(shift==3)
        break;
       if(shift!=1&&2&&3){
        printf("Exiting.........");
        break;

        }

return 0;
}
}




Quote:
Originally Posted by b49P23TIvg
Nice ideas, but this is not how c works. The expression
a && b
produces 0 or 1. It does not mean "do this function to both items".

Handling both files together is a good idea, to the extent that it's possible. You could make an array of FILE*infiles[2] and loop over that array. Without that array, using infile1 and infile2 of your program, you'll need to write more code treating the files separately. For example:

Code:
  /* open an existing file for reading */
  infile1 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\owner.txt", "r");
  if(NULL == infile1)		/* quit if the file does not exist */
    return 1;
  infile2 = fopen("C:\\Users\\slemma\\Desktop\\FiC++\\MyC++\\property.txt", "r");
  if(NULL == infile2) {		/* quit if the other file does not exist */
    fclose(infile1);		/* this file was opened, close it */
    return 2;
  }

  /* Get the number of bytes in the owner file*/
  fseek(infile1, 0L, SEEK_END);
  numbytes_in_owner = ftell(infile1);
  fseek(infile1, 0L, SEEK_SET);

Reply With Quote
  #10  
Old October 22nd, 2012, 07:43 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 118 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 18 h 48 m 29 sec
Reputation Power: 44
Code:
fscanf(infile1, "%s,%d,%s,%d", &owner.number, owner.name,owner.email,&owner.phone);             


Is the above code correct? You're reading owner.number as a char and owner.name as a int. Shouldn't it be vice versa?

Code:
 printf("owner number:%s - name:%d - email:%s - phone:%d",&owner.number, owner.name,owner.email,&owner.phone); 


Same thing with the above code.

Reply With Quote
  #11  
Old October 22nd, 2012, 07:51 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
it's not working. you lie.
Code:
/* reset the file position indicator to
the beginning of the file */
fseek(infile1, 0L, SEEK_SET);   /*****************you already moved to the beginning of the files*****************/
fseek(infile2, 0L, SEEK_SET);

/* grab sufficient memory for the
buffer to hold the text */
buffer = (char*)calloc(numbytes, sizeof(char)); /***************** using uninitialized numbytes ****************/
//buffer = malloc(numbytes * sizeof(char));     /***************** I won't tell you again ****************/

/* memory error */
if(buffer == NULL)                      /***************** if you want a job sometime in the future, write *****************/
    return 1;                           /***************** if (NULL == buffer) ****************/

/* copy all the text into the buffer */
fread(buffer, sizeof(char), numbytes, infile1);
fread(buffer, sizeof(char), numbytes, infile2); /***************** reading into same memory overwrites what you just read. **************** almost certainly a mistake ****************/

Reply With Quote
  #12  
Old October 22nd, 2012, 09:06 PM
slemma slemma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 slemma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 8 sec
Reputation Power: 0
you see one of the causes of conflict is perception. or you may have other agenda. I am here to learn, there is no reason to lie, if you were nice and ask what part is working, i would tell you. The opening file is working but the reading in to array of structs is what is not working. You do not have to reply anymore, the other guy knows exactly what is missing, he/she understood my issue, perhaps he/she has the other additional knowledge, other than the technical part, which more employers require.

God Bless you!


Quote:
Originally Posted by b49P23TIvg
it's not working. you lie.
Code:
/* reset the file position indicator to
the beginning of the file */
fseek(infile1, 0L, SEEK_SET);   /*****************you already moved to the beginning of the files*****************/
fseek(infile2, 0L, SEEK_SET);

/* grab sufficient memory for the
buffer to hold the text */
buffer = (char*)calloc(numbytes, sizeof(char)); /***************** using uninitialized numbytes ****************/
//buffer = malloc(numbytes * sizeof(char));     /***************** I won't tell you again ****************/

/* memory error */
if(buffer == NULL)                      /***************** if you want a job sometime in the future, write *****************/
    return 1;                           /***************** if (NULL == buffer) ****************/

/* copy all the text into the buffer */
fread(buffer, sizeof(char), numbytes, infile1);
fread(buffer, sizeof(char), numbytes, infile2); /***************** reading into same memory overwrites what you just read. **************** almost certainly a mistake ****************/

Reply With Quote
  #13  
Old October 22nd, 2012, 09:23 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
I agree that G4143 gives valuable insight. I didn't bother to check BobS0327 because the comments were so blatant they almost had to be correct.

On the other hand, you said
Quote:
Originally Posted by slemma
I got it to work
And there is no way the code you posted solves the problem you posted. Of course, you may have meant by "it to work" that you successfully clipped your toenails without cutting badly yourself.

If you are truly here to learn, read your own program!

Reply With Quote
  #14  
Old October 22nd, 2012, 09:55 PM
slemma slemma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 slemma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 8 sec
Reputation Power: 0
lol.....you sound like you are running for washington office. Why do you have to cut the sentence to make it look like i said it worked. This is what i exactly said "Thanks for your help. I got it to work but now i am trying to right it in to an array of structs but output is not what i expected. I do not know what i am doing wrong. Below is the code: Any tip is appreciated." if it worked why bother to post it. Look, it is about 9pm mountain time, go get good movie, make some herbal tea and enjoy.

God Bless you!



Quote:
Originally Posted by b49P23TIvg
I agree that G4143 gives valuable insight. I didn't bother to check BobS0327 because the comments were so blatant they almost had to be correct.

On the other hand, you said
And there is no way the code you posted solves the problem you posted. Of course, you may have meant by "it to work" that you successfully clipped your toenails without cutting badly yourself.

If you are truly here to learn, read your own program!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Load_data to memory

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap