Adding to an array in an external file, and adding elements to it.


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains:


Code:
char *testarray[]={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};

I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile the source code. Is this possible?

--- Post updated at 09:23 PM ---

I should also mention, that I am using
Code:
#include "array.txt"

with this file.
# 2  
Old 1 Week Ago
If you keep a file like this, it's much easier to add to:
Code:
Zero
One
Two
Three
Four
Five
Six
Seven
Eight
Nine

Then you translate it into an array with
Code:
( echo "static char *testarray[]={" ; xargs printf "\"%s\"," < arr ; echo "};" ) > array.h

It ending in a comma is allowable in C, which makes this easier.

The 'static' is to prevent linker errors if you use this in more than one file.
# 3  
Old 1 Week Ago
Ok. Thank you for that. But, I can't quite understand how I implement that into my code...
# 5  
Old 1 Week Ago
Code:
cat text.txt
>the table stands at the window

Code:
cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	int i;
//	char **testarr = (char**)malloc(sizeof(char*));
	char *dl = ", \t\n";
	char *word;
	FILE *file;
	file = fopen("text.txt", "r");

	fseek(file, 0l, SEEK_END);
	long size = ftell(file);
	char *buf = (char*)malloc(size * sizeof(char));
	fseek(file, 0l, SEEK_SET);
	fread(buf, sizeof(char), size, file);
	fclose(file);

	char **testarr = (char**)malloc(size * sizeof(char*));
	word = strtok(buf, dl);
	for(i=0; word != NULL; i++) {
		testarr[i] = (char*)malloc(strlen(word));
		strcpy(testarr[i], word);
		word = strtok(NULL, dl);
	}
        free(buf);

	for(int j = 0; j < i; j++) {
		printf("%s\n", testarr[j]);
                free(testarr[j]);
        }
        free(testarr);
}

Code:
./test
the
table
stands
at
the
window

--- Post updated at 10:46 ---

fixed the code, commented out the wrong line Smilie

--- Post updated at 11:41 ---

Something I am confused, I can not properly allocate memory for addresses in a dynamic array

--- Post updated at 12:34 ---

I fixed in a dynamic array.
But in the strtok function, the valgrind shows one error
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	int i;
	char *dl = ", \t\n";
	char *word;
	FILE *file;
	file = fopen("text.txt", "r");

	fseek(file, 0l, SEEK_END);
	long size = ftell(file);
	char *buf = (char*)malloc(size * sizeof(char) + 1);
	fseek(file, 0l, SEEK_SET);
	fread(buf, sizeof(char), size, file);
	fclose(file);

	char **testarr = (char**)malloc(sizeof(char*));
	word = strtok(buf, dl);
	for(i=0; word != NULL; i++) {
		testarr = (char**)realloc(testarr, sizeof(char*) + sizeof(char*) * i);
		testarr[i] = (char*)malloc(strlen(word) + 1);
		strcpy(testarr[i], word);
		word = strtok(NULL, dl);
	}
	free(buf);

	for(int j = 0; j < i; j++) {
		printf("%s\n", testarr[j]);
		free(testarr[j]);
	}
	free(testarr);
}

--- Post updated at 13:16 ---

Collected the program with debug information and Valgrind calmed down Smilie

--- Post updated at 13:27 ---

Add words to the file
Code:
cat text.txt
the table stands at the window
and the picture hang on the wall

Run without recompiling
Code:
./test
the
table
stands
at
the
window
and
pucture
hung
on
the
wall


Last edited by nezabudka; 1 Week Ago at 06:49 AM..
# 7  
Old 1 Week Ago
Thanks to everyone for their help. Now, i'd like to know, if possible, how do I display this array?

--- Post updated at 07:49 PM ---

Let me elaborate a little more on what i'd like to see... I'd like to be able to traverse the elements in the array with the arrow keys. That's essentially what i'm trying to do. I already have the code ready to accomplish this. I just need to be able to feed the function the name of the array. And that's it.


Thanks.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Adding Two Array in shell script mukulverma2408 Shell Programming and Scripting 8 09-07-2017 05:19 PM
Solaris - Sendmail - Adding .com.gr in external senders e-mails Mcasim Solaris 2 08-11-2017 04:36 AM
Help reading the array and sum of the array elements nishantrefound Shell Programming and Scripting 1 08-13-2016 07:43 AM
Adding an element to a bash array with a variable LMHmedchem Shell Programming and Scripting 4 03-11-2015 10:00 AM
How can we assign value to an array variable from an external file? mukulverma2408 Shell Programming and Scripting 6 07-12-2012 04:31 PM
Adding results of a find to an array Fly_Moe Shell Programming and Scripting 2 07-28-2011 03:31 PM
Help in adding in csv file mariselvam UNIX for Advanced & Expert Users 1 05-13-2011 03:03 AM
Adding new lines to a file + adding suffix to a pattern hemo21 Shell Programming and Scripting 7 06-14-2010 12:19 AM
Problem adding into an array field!!! talk2pawee Shell Programming and Scripting 5 06-11-2009 02:39 AM
Adding external disk array to a live server sbk1972 Solaris 1 04-17-2009 10:42 AM
Adding array element in KSH Shoeless_Mike Shell Programming and Scripting 3 12-17-2008 03:43 PM
Search array elements as file for a matching string mkbaral Shell Programming and Scripting 1 09-18-2008 04:20 AM
adding existing disks to a 3510 array malcqv Solaris 2 12-21-2007 03:40 AM
Help on adding file sizes llsmr777 UNIX for Dummies Questions & Answers 1 09-18-2007 02:58 PM
Adding from a file int007 Solaris 2 03-22-2005 07:05 PM