Writing to an array December 23, 2008 09:34PM | Registered: 16 years ago Posts: 37 |
char *charArray[8]; int t = 0; if ( in != NULL ) { char line[1024]; while ( fgets ( line, sizeof line, in ) != NULL ) { size_t i = strspn ( line, " \t\n\v" ); if ( (line == '#') || (line == ' ') ) { //if ( (line == '#') ) { continue; } //charArray[t] = line; // this doesn't seem to work strcpy(charArray[t], line); // this also doesn't seem to work t=t+1; } }
Re: Writing to an array December 23, 2008 09:39PM | Admin Registered: 16 years ago Posts: 5,132 |
Re: Writing to an array December 23, 2008 09:40PM | Registered: 16 years ago Posts: 37 |
Re: Writing to an array December 23, 2008 09:42PM | Registered: 16 years ago Posts: 13 |
Re: Writing to an array December 23, 2008 10:17PM | Admin Registered: 16 years ago Posts: 5,132 |
3 is the maximum amount of members to be copied. If more members are copied than the maximum amount than remaining members are simply not copied. Also remember that these are c style strings so there is an extra member in every string.Quote
SpaceJumpstrncpy(charArray[t], line, 3);
What is the 3 for?
//charArray[t] = line; // this doesn't seem to work
char Array [t] [line];
Re: Writing to an array December 23, 2008 10:21PM | Registered: 16 years ago Posts: 13 |
#include#include #include #define INFILE "fstab" #define MAXLINE 1024 typedef struct _list { struct _list *next; char *data; } list; static list *add_line(list *l, const char *s) { if(!l) { l = malloc(sizeof(list)); l->data = strdup(s); l->next = NULL; } else { l->next = malloc(sizeof(list)); l->data = strdup(s); l = l->next; } return l; } int main(void) { FILE *f; list l; list *lp; char line[MAXLINE]; f = fopen(INFILE, "r"); lp = &l; while(fgets(line, MAXLINE-1, f)) { line[MAXLINE-1] = '\0'; if(line[0] == '#') continue; lp = add_line(lp, line); } lp = &l; while(lp = lp->next) printf("%s", lp->data); fclose(f); return 0; }
Re: Writing to an array December 24, 2008 02:11AM | Registered: 16 years ago Posts: 73 |
Quote
SpaceJump
What am I doing wrong?
char *charArray[8];The above allocates space to hold 8 pointers to a char. Your program will need to provide the memory for the data you plan to store at each of these addresses. The string functions themselves don't allocate any memory (expect for strdup). One common way to allocate strings, is to use the function strdup(). Its prototype looks like this:
#include <stdlib.h> #include <stdio.h> #include <string.h> char *charArray[255]; //maybe rename this lineArray, as it's an array of lines int main(int argc, char *argv[]) { char line[1024]; int current_line_num = 0; int i; FILE *fp = fopen("test.txt", "r"); if (fp != NULL) { while(fgets(line, sizeof line, fp) != NULL) { //skip lines which begin with a '#' if (line[0] == '#') continue; //make a copy of the current line using strdup (string duplicate) charArray[current_line_num] = strdup(line); current_line_num++; } //print results for(i = 0; i <current_line_num; i++) printf("%s", charArray); //release memory allocated by strdup's for(i = 0; i <current_line_num; i++) if (charArray != NULL) free(charArray); } return 0; }
Re: Writing to an array December 24, 2008 02:13AM | Registered: 16 years ago Posts: 13 |
Re: Writing to an array December 24, 2008 02:14AM | Registered: 16 years ago Posts: 37 |
Re: Writing to an array December 24, 2008 03:27AM | Registered: 16 years ago Posts: 73 |
Quote
lordzid
I prefer mine, naturally.
char line[MAXLINE]; ... while(fgets(line, MAXLINE-1, f))Why do you pass in MAXLINE -1, rather than just MAXLINE? This is likely related to why you do this:
line[MAXLINE-1] = '\0';Why do you do the above? As I understand fgets(), it will always place a '\0' at the end of the data it reads in.
Quote
man fgets
char *fgets(char *s, int size, FILE *stream);
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.
Re: Writing to an array December 24, 2008 04:08PM | Registered: 16 years ago Posts: 37 |
Re: Writing to an array December 24, 2008 04:18PM | Registered: 16 years ago Posts: 13 |
static void free_list(list *l) { if(l->next) free_list(l->next); free(l->data); free(l); }
static void process_list(list *l) { if(l->next) { printf("%s", l->data); free_list(l->next); } free(l->data); free(l); }
Re: Writing to an array December 24, 2008 05:03PM | Registered: 16 years ago Posts: 73 |