How to replace text file and use an array instead?

Feb 21, 2011 at 11:15pm
Hello Everybody,

This program works and it use a text file named yes.txt with these lines and no blank line at the beginning or end of file:
sing
Sing
SANG!!!
yes
no
nope YES nope YES

Now that I know how to work with vector a bit I want to eliminate this external file and do the same thing with the same text being inside the program in a string array mainly for the knowledge of how-to. I thought it would be simple, I included a array "string zfile[6]" to replace the external text file but I don't know how to make it work using this array. Could someone show me the syntax to use to make this happen. I'm hoping to stay close as possible to what I already have so that I can understand the changes that will be needed ... or do the entire set-up has to be changed completly. Thanks in advance


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

string zfile[6]={"sing", "Sing", "SANG!!!", "yes", "no", "nope YES nope YES",};
//string zfile[6];


int main()
  {
     ifstream xfile("yes.txt");

        if ( !xfile )
        {     cerr << "File does not exist.\n\n"; system("pause");  exit( 1 );  }

        vector<string> lines;
        string line;

        while (getline(xfile, line))
        {
        lines.push_back(line);
                                
        cout << "results:  " <<  line  << endl;
        }

        xfile.close();

        system("pause");

 }
Feb 21, 2011 at 11:22pm
To use your array of strings, you can initialize lines like so:
 
vector<string> lines(zfile, zfile + 6);


Is that what you are asking?
Last edited on Feb 21, 2011 at 11:23pm
Feb 22, 2011 at 12:59am
Is that what you are asking?


I mainly need to know how to do this using C and C++ standard string libraries for given array of strings. This will leave me with a clear understanding of how to switch back and forth from external files usage to an internal arrays of strings when needed for difference types of coding project.

I will certainly be trying this Array of vector<string> in a few minutes which is a BIG PLUS. :) But I don't need to get to fancy with vector just yet. I just want to end-up using the most fail-proof method possible. C++ people say use C++ strings but if C-strings are safer I am not so lazy or afraid to work with them but if vector is the the way to go than I will dedicate my all into it. Give me the word and I am there.

But for now it's all about how to translate the code from using an external file to using a build-in arrays. While googling I found that many people need help to do just the reverse of what I am after. Thanks a lot for the reply. I can't wait to try it ...
Feb 22, 2011 at 1:36am
PS: After just doing more googling, I think my exampe may be mis-leading, but I hope many of you still get my point. Another thing I am a noob and I don't know how to insert this vector<string> lines(zfile, zfile + 6); thing yet and other changes needed. It's a lot more to it than I thought.

Thanks again
Feb 22, 2011 at 3:44am
Got it!!!
Google and the pages here is the GREATEST :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <vector>
using namespace std;


void ShowTime (const vector<string>& j)
     {
     for (int i = 0; i < j.size(); ++i)
     {
     cout << "  " << j[i] << endl;    }   }

int main ()
    {
    vector<string> str1(10);
    cout << endl << endl;
    str1[0] = "sing_C++";
    str1[1] = "sing";
    str1[2] = "Sing_C++";
    str1[3] = "or";
    str1[4] = "to";
    str1[5] = "the";
    str1[6] = "slammer";
    str1[7] = "you";
    str1[8] = "GO!";   
    str1[9] = " ";
                 ShowTime (str1);

    vector<string> str2;// vector with size zero, will grow to accommodate

    str2.push_back(" ");
    str2.push_back("la");
    str2.push_back("la");
    str2.push_back("la");
    str2.push_back("lalalaaaa");
                 ShowTime (str2);

    cout << endl << endl;

system("pause");
system ("CLS");
//return main();
}


This is pure C++... Now how would I do it in pure C ...
Feb 22, 2011 at 9:52am
You have to write the string and vector classes from scratch. I have made some effort, but it is laborious :(

main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef __MAIN__
#define __MAIN__

/************ Implementation of std::string<char> in C ***********/

struct String
{
	char *elements;
};

void String_constructor (struct String *this);
void String_copyConstructor (struct String *this, const struct String *o);
void String_destructor (const struct String *this);
int String_size (const struct String *this);
const char *String_get (const struct String *this);
void String_assign (struct String *this, const char *str);

/************ Implementation of std::vector<std::String> in C ***********/

struct StringVector
{
	struct String *elements;
	size_t size;
	size_t capacity;
};

void StringVector_constructor (struct StringVector *this);
void StringVector_destructor (const struct StringVector *this);

size_t StringVector_push_back (struct StringVector *this,
			       const struct String *string);

const struct String *StringVector_getElement (const struct StringVector *this,
					      size_t i);

size_t StringVector_size (const struct StringVector *this);

#endif 


main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "main.h"

/************ Implementation of std::string<char> in C ***********/
/********** Constructor/Destructor ***********/

void String_constructor (struct String *this)
{
	printf ("%p: String_constructor\n", this);
	this->elements = malloc (1);
	strcpy (this->elements, "");
}

void String_copyConstructor (struct String *this, const struct String *o)
{
	printf ("%p: String_copyConstructor: from %p\n", this, o);
	this->elements = malloc (strlen (o->elements) + 1);
	strcpy (this->elements, o->elements);
}

void String_destructor (const struct String *this)
{
	printf ("%p: String_destructor\n", this);
	free (this->elements);
}

/********** Capacity ***********/

int String_size (const struct String *this)
{
	return strlen (this->elements);
}

/********** Element access ***********/

const char *String_get (const struct String *this)
{
	return this->elements;
}

/********** Modifier ***********/

void String_assign (struct String *this, const char *str)
{
	printf ("%p: String_assign: %s\n", this, str);
	this->elements = (char *)realloc (this->elements, strlen(str) + 1);
	strcpy (this->elements, str);
}

/************ Implementation of std::vector<std::String> in C ***********/
/********** Constructor/Destructor ***********/

void StringVector_constructor (struct StringVector *this)
{
	printf ("%p: StringVector_constructor\n", this);
	this->elements = malloc (1);
	this->size = 0;
	this->capacity = 1;
}

void StringVector_destructor (const struct StringVector *this)
{
	size_t i;
	printf ("%p: StringVector_destructor\n", this);

	for (i = 0; i < this->size; i++)
		String_destructor (&this->elements[i]);

	free (this->elements);
}

/********** Modifier ***********/

size_t StringVector_push_back (struct StringVector *this,
			       const struct String *string)
{
	size_t i;
	struct String *relocatedElements;
	printf ("%p: StringVector_push_back\n", this);

	if (this->size == this->capacity) {

		printf ("%p: StringVector_push_back: re-allocating\n", this);
		this->capacity = 2 * this->capacity;

		relocatedElements = (struct String *)
		malloc (this->capacity * sizeof (struct String));

		for (i = 0; i < this->size; i++)
			String_copyConstructor (&relocatedElements[i],
						&this->elements[i]);

		for (i = 0; i < this->size; i++)
			String_destructor (&this->elements[i]);

		free (this->elements);
		this->elements = relocatedElements;

		printf ("%p: StringVector_push_back: re-allocation complete\n",
			this);
	}

	String_copyConstructor (&this->elements[this->size], string);
	this->size++;
}

/********** Element access ***********/

const struct String *StringVector_getElement (const struct StringVector *this,
					      size_t i)
{
	return &this->elements[i];
}

/********** Capacity ***********/

size_t StringVector_size (const struct StringVector *this)
{
	return this->size;
}

/********************* Main program ***********************/

void showTime (const struct StringVector *vector)
{
	size_t i;

	for (i = 0; i < StringVector_size (vector); i++)
		printf ("%s\n",
		String_get (StringVector_getElement (vector, i)));
}

int main (void)
{
	struct StringVector stringVector;
	StringVector_constructor (&stringVector);

	struct String str1;
	String_constructor (&str1);
	String_assign (&str1, "Hi");
	StringVector_push_back (&stringVector, &str1);
	String_destructor (&str1);

	struct String str2;
	String_constructor (&str2);
	String_assign (&str2, "Hello");
	StringVector_push_back (&stringVector, &str2);
	String_destructor (&str2);

	showTime (&stringVector);
	StringVector_destructor (&stringVector);
	return 0;
}


The output goes as:


0xbf9b2704: StringVector_constructor
0xbf9b2700: String_constructor
0xbf9b2700: String_assign: Hi
0xbf9b2704: StringVector_push_back
0x806d008: String_copyConstructor: from 0xbf9b2700
0xbf9b2700: String_destructor
0xbf9b26fc: String_constructor
0xbf9b26fc: String_assign: Hello
0xbf9b2704: StringVector_push_back
0xbf9b2704: StringVector_push_back: re-allocating
0x806d038: String_copyConstructor: from 0x806d008
0x806d008: String_destructor
0xbf9b2704: StringVector_push_back: re-allocation complete
0x806d03c: String_copyConstructor: from 0xbf9b26fc
0xbf9b26fc: String_destructor
Hi
Hello
0xbf9b2704: StringVector_destructor
0x806d038: String_destructor
0x806d03c: String_destructor


Watch the pointers to carefully to track how the classes/structures are created, copied and destroyed.
Topic archived. No new replies allowed.