446,389 Members | 1,881 Online
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

# write string to an array

 P: n/a Suppose I have the following function in my program: void ExtractData(Ind *AM) { int i,j; char str[255]; char c; FILE *ext=fopen("test.out","r"); //suppose I have N line each with M digits (in here M=5) like: 1 2 2 1 3 for(i=0; i
5 Replies

 P: n/a MJK wrote: Suppose I have the following function in my program: void ExtractData(Ind *AM) We don't know what type `Ind` is. { int i,j; char str[255]; char c; FILE *ext=fopen("test.out","r"); Check that `ext` isn't null. [Isn't "test.out" a misleading name for an input file?] //suppose I have N line each with M digits (in here M=5) like: 1 2 2 1 3 With spaces between the digits? for(i=0; i

 P: n/a In article <11**********************@g37g2000prf.googlegroups .com>, MJK

 P: n/a MJK wrote: > Suppose I have the following function in my program: void ExtractData(Ind *AM) { int i,j; char str[255]; char c; FILE *ext=fopen("test.out","r"); //suppose I have N line each with M digits (in here M=5) like: 1 2 2 1 3 for(i=0; i #include #include #define M 5 #define N 11 #define LENGTH 254 #define str(x) # x #define xstr(x) str(x) typedef struct { char S[M]; } Ind; void ExtractData(Ind *AM); int main(void) { Ind Cool[N]; size_t array, letter; ExtractData(Cool); for (array = 0; array != N; ++array) { for (letter = 0; letter != M; ++letter) { putchar(Cool[array].S[letter]); putchar(' '); } putchar('\n'); } return 0; } void ExtractData(Ind *AM) { int rc, i, j; char str[LENGTH + 1], *p; FILE *ext; ext = fopen("test.out", "r"); if (ext == NULL) { puts("ext == NULL"); exit(EXIT_FAILURE); } /* ** suppose I have N line each with M digits ** (in here M=5) like: 1 2 2 1 3 */ for (i = 0; N i; ++i) { rc = fscanf(ext, "%" xstr(LENGTH) "[^\n]%*[^\n]", str); if (!feof(ext)) { getc(ext); } if (rc == 0) { str[0] = '\0'; } if (rc == EOF) { puts("rc equals EOF"); exit(EXIT_FAILURE); } /* ** This line read the whole 5 digits ** I want each of those five digits become an element of ** "AM[i].S[j]" array as below: */ p = str; for (j = 0; M j; ++j) { while (!isdigit(*p)) { if (*p == '\0') { puts("*p == '\0'"); exit(EXIT_FAILURE); } ++p; } AM[i].S[j] = *p++; } } } /* END test2.c */ -- pete Jun 6 '07 #4

 P: n/a pete wrote: > MJK wrote: FILE *ext=fopen("test.out","r"); //suppose I have N line each with M digits (in here M=5) like: 1 2 2 1 3 /* BEGIN test2.c */ #include #include #include #define M 5 #define N 11 I used an 11 line test file, in case you're wondering where that number came from. 1 2 2 1 3 1 2 3 1 3 1 2 5 1 3 1 2 2 1 3 1 9 2 1 3 1 4 2 1 3 1 2 0 1 3 1 2 2 7 3 3 2 2 1 3 1 2 2 1 8 6 2 2 1 3 -- pete Jun 6 '07 #5

 P: n/a pete wrote: void ExtractData(Ind *AM) { int rc, i, j; char str[LENGTH + 1], *p; FILE *ext; ext = fopen("test.out", "r"); if (ext == NULL) { puts("ext == NULL"); exit(EXIT_FAILURE); } /* ** suppose I have N line each with M digits ** (in here M=5) like: 1 2 2 1 3 */ for (i = 0; N i; ++i) { rc = fscanf(ext, "%" xstr(LENGTH) "[^\n]%*[^\n]", str); if (!feof(ext)) { getc(ext); } if (rc == 0) { str[0] = '\0'; } if (rc == EOF) { puts("rc equals EOF"); exit(EXIT_FAILURE); } /* ** This line read the whole 5 digits ** I want each of those five digits become an element of ** "AM[i].S[j]" array as below: */ p = str; for (j = 0; M j; ++j) { while (!isdigit(*p)) { if (*p == '\0') { puts("*p == '\0'"); exit(EXIT_FAILURE); } ++p; } AM[i].S[j] = *p++; } } fclose(ext); } /* END test2.c */ -- pete Jun 6 '07 #6

### This discussion thread is closed

Replies have been disabled for this discussion.