C
c++dummy
I got this project for my class and I'm totally lost as to how to copy
the 1d array with the bone name into a 2d array using this supposed
strncpy function I'm supposed to create. I believe the teacher wants us
to make the function and not use the library function.
Here's the info:
Write a C++ program that
Opens the file for input using the ifstream facility (see example
program from class).
Declares a two dimensional character array to store the bones from the
input file.
Reads each line of the file and stores the bone in the array position
indicated by the number at the start of the line (e.g., toe is the
first element, foot is the second, etc.; remember that C++ array
indices start with 0). As you process the file, read each bone into a
separate character array and then use the strncpy() function you will
write (see below) to copy the bone name into the main array.
Loops through the main bone array and builds up a long character string
saying which bone is connected to which (e.g., "The toe bone is
connected to the foot bone\nThe foot bone is connected to the ....").
The string should contain a newline character separating each statement
of connectedness. To do this, use the strcat() function you will write
(see below).
Prints the long character string of connectedness to the console.
Your program will use following three functions that you will write.
These functions should use pointer arithmetic rather than array
notation.
void strncpy(char *dest, char *src, int numchr);
Copies the string pointed to by src to the character array pointed to
by dest. numchr is the number of characters to copy. After the copy
operation, dest should be null terminated.
int strlen(char *str)
returns the number of characters in the null-terminated string pointed
to by str.
void strcat(char *dest, char *src)
appends the null-terminated string pointed to by src to the
null-terminated string pointed to by dest. Moves dest's terminating
null to the end of the new string.
Hints:
When you call strncpy, you can use strlen to determine the length of
src
Make sure you make your character arrays big enough
AND HERE IS THE TEXT FILE CONTENT BEING READ:
2 foot
4 leg
10 head
3 ankle
6 thigh
7 hip
1 toe
5 knee
8 back
9 neck
the 1d array with the bone name into a 2d array using this supposed
strncpy function I'm supposed to create. I believe the teacher wants us
to make the function and not use the library function.
Here's the info:
Write a C++ program that
Opens the file for input using the ifstream facility (see example
program from class).
Declares a two dimensional character array to store the bones from the
input file.
Reads each line of the file and stores the bone in the array position
indicated by the number at the start of the line (e.g., toe is the
first element, foot is the second, etc.; remember that C++ array
indices start with 0). As you process the file, read each bone into a
separate character array and then use the strncpy() function you will
write (see below) to copy the bone name into the main array.
Loops through the main bone array and builds up a long character string
saying which bone is connected to which (e.g., "The toe bone is
connected to the foot bone\nThe foot bone is connected to the ....").
The string should contain a newline character separating each statement
of connectedness. To do this, use the strcat() function you will write
(see below).
Prints the long character string of connectedness to the console.
Your program will use following three functions that you will write.
These functions should use pointer arithmetic rather than array
notation.
void strncpy(char *dest, char *src, int numchr);
Copies the string pointed to by src to the character array pointed to
by dest. numchr is the number of characters to copy. After the copy
operation, dest should be null terminated.
int strlen(char *str)
returns the number of characters in the null-terminated string pointed
to by str.
void strcat(char *dest, char *src)
appends the null-terminated string pointed to by src to the
null-terminated string pointed to by dest. Moves dest's terminating
null to the end of the new string.
Hints:
When you call strncpy, you can use strlen to determine the length of
src
Make sure you make your character arrays big enough
AND HERE IS THE TEXT FILE CONTENT BEING READ:
2 foot
4 leg
10 head
3 ankle
6 thigh
7 hip
1 toe
5 knee
8 back
9 neck