#include <stdio.h>
#include <string.h>
#define LEN_STR 100
#define MAX_ROWS 10000
int main(void)
{
int i = 0;
FILE *ofp;
FILE *ifp;
char str [LEN_STR+1];
char *records[MAX_ROWS];
ifp = fopen("FishFood.txt","r");
if(ofp == NULL)
{
printf("Error in opening the file");
}
else
{
ofp = fopen("Fishy2.txt","w"); // ignore this for now
if(ifp == NULL)
{
printf("Error in opening the file");
}
else
{
while(!feof(ifp)&& i < MAX_ROWS){
fgets(str, sizeof(str), ifp);
strcpy(records, str); //copy string into pointer to string char's
printf("%s", records); // print off current record
*(records+i); // advance the pointer address
i++;
}
fclose(ifp);
printf("%s", records); // only prints the last string on the file list
//, which makes me think the pointer address does not advance
// and each string just gets replaced by the newer one
} // I'm expecting to return the first string copied into records
}
return 0;
}
copy a string into pointer array to char
Page 1 of 113 Replies - 1884 Views - Last Post: 19 March 2011 - 01:56 PM
#1
copy a string into pointer array to char
Posted 19 March 2011 - 11:55 AM
Replies To: copy a string into pointer array to char
#2
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:09 PM
while(string[i]!='\0'){ // \0 is the mark that tells you that your string is ended
*char[i]=string[i];
i++;
}
This post has been edited by Patrunjel: 19 March 2011 - 12:09 PM
#3
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:21 PM
Patrunjel, on 19 March 2011 - 12:09 PM, said:
while(string[i]!='\0'){ // \0 is the mark that tells you that your string is ended
*char[i]=string[i];
i++;
}
hum, dunno if this makes a difference or not, but one line of strings reads like this.
TetraFin Goldfish Flakes - 2.20 Pounds,24.00,24.00
I want to put each line into a memory address for further manipulation afterwards
#4
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:27 PM
This post has been edited by Patrunjel: 19 March 2011 - 12:28 PM
#5
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:30 PM
*(records+i); // advance the pointer address
isn't doing what you think its doing. If you were to use that in your strcpy call then my above statement would be invalid. It's only advancing the pointer temporarily.
In addition to all of the above you still need to allocate space to hold the strings you want to copy.
Quote
??? You can store ten letters in there, and a collection of letters is a "string".
#6
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:31 PM
#7
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:34 PM
Your printf on line 43 will only print one C-string. If you want it to print more than one you will need to put it into a loop.
Also when I compile your code I get the following warnings/errors:
Quote
main.c|24|warning: (this will be reported only once per input file)|
main.c||In function ‘main’:|
main.c|35|warning: passing argument 1 of ‘strcpy’ from incompatible pointer type|
/usr/include/string.h|127|note: expected ‘char * __restrict__’ but argument is of type ‘char **’|
main.c|36|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|37|warning: statement with no effect|
main.c|44|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|18|warning: ‘ofp’ is used uninitialized in this function|
||=== Build finished: 0 errors, 7 warnings ===|
Jim
#8
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:35 PM
Patrunjel, on 19 March 2011 - 01:31 PM, said:
Not in this case, he hasn't allocated any space for the strings. Currently all we have is an array of ten char pointers. We have no idea what each can hold because we haven't defined it yet.
#9
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:42 PM
Patrunjel, on 19 March 2011 - 12:27 PM, said:
what? char[10] is a char string with a place for "huhsaywhat"
a char can only store one letter or character!
you just made a typo right? ;-)
thanks for trying to help out though.
KYA, on 19 March 2011 - 12:30 PM, said:
*(records+i); // advance the pointer address
isn't doing what you think its doing. If you were to use that in your strcpy call then my above statement would be invalid. It's only advancing the pointer temporarily.
In addition to all of the above you still need to allocate space to hold the strings you want to copy.
with malloc , or otherwise it would segfault right KYA ?
#10
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:47 PM
#11
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:50 PM
jimblumberg, on 19 March 2011 - 12:34 PM, said:
Your printf on line 43 will only print one C-string. If you want it to print more than one you will need to put it into a loop.
Also when I compile your code I get the following warnings/errors:
Quote
main.c|24|warning: (this will be reported only once per input file)|
main.c||In function ‘main’:|
main.c|35|warning: passing argument 1 of ‘strcpy’ from incompatible pointer type|
/usr/include/string.h|127|note: expected ‘char * __restrict__’ but argument is of type ‘char **’|
main.c|36|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|37|warning: statement with no effect|
main.c|44|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|18|warning: ‘ofp’ is used uninitialized in this function|
||=== Build finished: 0 errors, 7 warnings ===|
Jim
thanks Jim and all the guys that looked into my problem!
this site is great because of you, thanks again.
#12
Re: copy a string into pointer array to char
Posted 19 March 2011 - 12:53 PM
Jim
#13
Re: copy a string into pointer array to char
Posted 19 March 2011 - 01:05 PM
jimblumberg, on 19 March 2011 - 12:34 PM, said:
Your printf on line 43 will only print one C-string. If you want it to print more than one you will need to put it into a loop.
Also when I compile your code I get the following warnings/errors:
Quote
main.c|24|warning: (this will be reported only once per input file)|
main.c||In function ‘main’:|
main.c|35|warning: passing argument 1 of ‘strcpy’ from incompatible pointer type|
/usr/include/string.h|127|note: expected ‘char * __restrict__’ but argument is of type ‘char **’|
main.c|36|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|37|warning: statement with no effect|
main.c|44|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|18|warning: ‘ofp’ is used uninitialized in this function|
||=== Build finished: 0 errors, 7 warnings ===|
Jim
I just put that in there as a test.
Thanks Jim, checking my compiler settings as we speak.
#14
Re: copy a string into pointer array to char
Posted 19 March 2011 - 01:56 PM
#include <stdio.h>
#include <string.h>
#define LEN_STR 100
#define MAX_ROWS 10000
char *copyString(const char *str) {
char *newStr;
/* your code here
just strcpy is not enough
you must malloc as well
*/
return newStr;
}
/* returns number read */
int readRecords(FILE *ifp, char *records[]) {
int count = 0;
while(!feof(ifp)&& count < MAX_ROWS){
char str[LEN_STR+1];
fgets(str, sizeof(str), ifp);
printf("read: %s", str);
records[count++] = copyString(str);
}
return count;
}
void printRecords(char *records[], int size) {
/* your code here
you must loop from 0..size-1 and show each record
*/
}
/* I can't see what you're doing with ofp yet, but we'll pretend */
void processOpenFiles(FILE *ifp, FILE *ofp) {
char *records[MAX_ROWS];
int count = readRecords(ifp, records);
printRecords(records, count);
/* perhaps do the same thing as printRecords, but print to file? */
}
int processFile(const char *inFileName, const char *outFileName) {
FILE *ifp = fopen(inFileName,"r");
if(ifp == NULL) {
printf("Error in opening the file");
} else {
FILE *ofp = fopen(outFileName,"w"); // ignore this for now
if(ofp == NULL) {
printf("Error in opening the file");
fclose(ifp);
} else {
processOpenFiles(ifp, ofp);
fclose(ifp);
fclose(ofp);
return 0;
}
}
return 1; // oops
}
int main() {
return processFile("FishFood.txt", "Fishy2.txt");
}
Hope this helps.
|
|

New Topic/Question
Reply




MultiQuote





|