954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to store data into array from file?

ifstream inputfile;
	cout << "enter the name of the file: ";
	cin >> filename;
	inputfile.open(filename);
	string temp;
	int i = 0;

	while(filename != NULL)
	{
		while(isalpha(filename))
		{
			temp[i++] = filename;
		}
		if(i > 0)
		{
			insert(temp, 1);
			i = 0;
		}
	}


this is my code, what am I doing wrong?

I'm creating a linked list(not a part of the question asked), I'm inserting all the words from a file into the list.

am I extracting the word the wrong way? someone please show me the light :)

homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

nvm, figured it out :)

homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

You're OK till the while loop - what's it supposed to be doing?

How about something like:

if( inputfile ) //file opened, let's do some work
{
   while ( inputfile >> temp ) //gets one word at a time
   {                            //if successful, store the word
           list.insert( temp );
           i++;    //just so you know how many you've inserted
    }
}

Once you've opened the file named in filename, you don't need to use that variable for anything.

isapha( ) takes single characters as its argument, not a string.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
nvm, figured it out :)


Care to share? Sure you got it right?

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
Care to share? Sure you got it right?


sure :)

void file(char filename[])
{
	List l;
	ifstream inputfile;
	cout << "enter the name of the file: ";
	cin >> filename;
	inputfile.open(filename);
	char temp[SIZE];
	string temp2;
	
	while(inputfile != NULL)
	{
		inputfile >> temp;
		int i = 0;
		while(temp != NULL)
		{
			temp[i] = tolower(temp[i]);
			i++;
		}
		temp2 = temp;
		l.insert(temp2, 1);
	}
	
}
homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

Sure this is working right?

how about while( temp[i] != NULL )

temp itself will never be NULL in this usage.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

Sure this is working right?

how about while( temp[i] != NULL )

temp itself will never be NULL in this usage.

thank you!! I was wondering why my program kept crashing.

homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You