Thread: Reading data into an array - how to?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Unhappy Reading data into an array - how to?

    Heya im attempting to work out how to read data from a text file into an array, after reading many sites and attempting it myself my brain is starting to hurt

    Say i had a text file containing 10 numbers on each line, and i wanted to read in the first 5 into 1 array, and the next 5 into a different array.

    How is that done?

    quick replies appreciated looks like im gonna be up all night heh

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Run a loop five times reading into each spot in the first array. Then do it again with the second array.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    So stuck!

    Well i guess i should say i dont have a clue how to even begin making an array!

    Here is what i have done, mainly getting the input output file open, i know it's a mess but i havent been doing this long.
    Code:
    #include <iostream.h>
    #include <string>
    #include <fstream>
    using namespace std;
    
    bool getInputFilename(char fname[])
    {
    	ifstream fin;
    
    	cout << "Please enter the filename for input : ";
    	cin >> fname;
    	
    	fin.open(fname, ios::nocreate);
    	
    	if (!fin.is_open())
    		return false;
    	
    	fin.close();
    	return true;
    }
    
    
    bool getOutputFilename(char fname[])
    {
    	ofstream fout;
    
    	cout << "Please enter the filename for output : ";
    	cin >> fname;
    
    	fout.open(fname);
    
    	if (fout.fail())
    		return false;
    
    	fout.close();
    	return true;
    
    }
    
    int firstArray(int x[])
    {
    	ifstream fin;
    	int x[];
    
    	for (int j=0;j<6;j++)
     {
        fin  >> x;
        
     }
    }
    
    
    
    
    
    void main()
    {
    	ifstream fin;
    	ofstream fout;
    
    	char IFname[20], OFname[20];
    
    	int levelTwo;
    
    
    	while (!getInputFilename(IFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	while (!getOutputFilename(OFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	fout.open(OFname);
    	fin.open(IFname);
    
    	cout << x  << endl;
    
    }
    coloured the array stuff i tried :s any help much appreciated
    Last edited by KellyWhite1; 01-08-2006 at 07:09 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    IFname and OFname are arrays. They hold 20 chars instead of 5 ints, but the same idea applies.

    To access a single element in an array, use the subscript operator. So if you had your loop with j as the index, you would read into the array at x[j].

    Also, make sure your loop only runs 5 times. From 0 to less than 6 runs six times.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Banging my head off the desk doesnt seem to help

    I'm trying to find out what i am doing wrong, the OFname and IFname are used in getting a valid filename from a user, what i need to know is how to get my array working to take 5 numbers from a text file and put them into a new array!

    The stuff i highlighted in green was my poor attempt to get data from a text file and put it into an array, can anyone enlighten me!?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You know how to declare an array, you did it with IFname and OFname. Do it again, but this time declare an array that holds 5 ints. That's the first step.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Another attempt

    Well i looked around a bit more added what i thought could work, but still nothing.

    why cant i output the numbers in the array to a new text file? Does the array even work did i do it right?

    Code:
    #include <iostream.h>
    #include <string>
    #include <fstream>
    using namespace std;
    
    
    
    bool getInputFilename(char fname[])
    {
    	ifstream fin;
    
    	cout << "Please enter the filename for input : ";
    	cin >> fname;
    	
    	fin.open(fname, ios::nocreate);
    	
    	if (!fin.is_open())
    		return false;
    	
    	fin.close();
    	return true;
    }
    
    
    bool getOutputFilename(char fname[])
    {
    	ofstream fout;
    
    	cout << "Please enter the filename for output : ";
    	cin >> fname;
    
    	fout.open(fname);
    
    	if (fout.fail())
    		return false;
    
    	fout.close();
    	return true;
    
    }
    
    int firstArray(int i, int array[5], int maxSize, int amountRead)
    {
    	ifstream fin;
    	ofstream fout;
    
    	while(fin>>array[amountRead]&& amountRead < maxSize)
    	{
    		amountRead++;
    	}
    
    	for (i=0; i < 5; i++)
    	{
    		fout << array[i] << endl;
    	}
    
    	cin.get();
    
    	return 0;
    }
    
    
    
    void main()
    {
    	ifstream fin;
    	ofstream fout;
    
    
    	char IFname[20], OFname[20];
    	
    
    
    
    	while (!getInputFilename(IFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	while (!getOutputFilename(OFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	fout.open(OFname);
    	fin.open(IFname);
    
    
    
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have a function, but you don't call that function, so it doesn't do anything.

    Your for loop that outputs array is correct. Use the same technique to read into the array.

    It looks like you are just copying code from different places without understanding what it does. Make sure you pick one thing to work on and understand the issue before moving on to the next thing. For example, first make an empty function. Then call that function from main. Then create an array that holds the ints. Then add code to open the input file. Then add code to read in 5 ints only from that file. Then add code to open up the output file. And so on and so on... There are other ways to do it, that is just an example. Do each step before you move on, and if you can, test each step to make sure it compiles and runs correctly.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Question Getting somewhere!

    Well i have finally sorted out my program so it will put numbers into an array, and also output them to a text file.

    The problem is it only inputs 1 set of numbers!
    Code:
    #include <iostream.h>
    #include <string>
    #include <fstream>
    using namespace std;
    
    
    
    bool getInputFilename(char fname[])
    {
    	ifstream fin;
    
    	cout << "Please enter the filename for input : ";
    	cin >> fname;
    	
    	fin.open(fname, ios::nocreate);
    	
    	if (!fin.is_open())
    		return false;
    	
    	fin.close();
    	return true;
    }
    
    
    bool getOutputFilename(char fname[])
    {
    	ofstream fout;
    
    	cout << "Please enter the filename for output : ";
    	cin >> fname;
    
    	fout.open(fname);
    
    	if (fout.fail())
    		return false;
    
    	fout.close();
    	return true;
    
    }
    
    void firstArray(ifstream& fin, int marks [], int arraySize)
    {
    
    
    	while (!fin.eof()) {
    
    
      	for (int i = 0; i < 10; i++) {
      		fin >> marks[i];
      					
    	}
    	
    	}
    }
    
    
    
    
    
    
    void main()
    {
    	ifstream fin;
    	ofstream fout;
    
    	char IFname[20], OFname[20];
    	int marks [10];
    
    
    	while (!getInputFilename(IFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	while (!getOutputFilename(OFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	fout.open(OFname);
    	fin.open(IFname);
    
    	firstArray(fin,marks,10);
    
    
         for(int i = 0; i < 10; i++)
              fout << marks[i] << endl;
        
    
    	fout.close();
    	fin.close();
    
    }

    The test file i used i just contained 10 numbers, however the text file data i want to use contains:

    name //can be 3 words seperated by spaces
    10 numbers // all ints

    so eg:

    Bla Bla Bla

    13 18 38 49 58 69 94 85 48 95

    Bleh Bleh Bleh

    83 47 38 49 03 27 45 48 04 58


    How would i change my code so it can output data in the form of:

    name - average of first 5 numbers - average of last 5 numbers
    name - average of first 5 numbers - average of last 5 numbers

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Look into using structs and stream-based array I/O instead of character I/O. Load the struct in using the array-based stream functions instead of the character functions.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Take the while (!fin.eof()) part out of your firstArray function. That will make that code read through the entire file (while not end-of-file). That's not what you want. That should be in main because you want to call that function over and over for each set of data.

    Why does your array hold 10 ints when you want to read in 5? Maybe you should have two arrays of 5 ints and another character array for the name. Pass all those to the function and let the function fill up the data for one set. Then output it and continue your loop in main.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Well i guess i should say i dont have a clue how to even begin making an array!
    C++: A Beginner's Guide

    http://www.amazon.com/gp/product/007...books&v=glance

  13. #13
    Registered User
    Join Date
    Jan 2006
    Location
    Jerusalem
    Posts
    11
    Hello!
    i'm a new programmer :$ and somehow i have alot of problems!
    can anyone tells me when do we exactly use arrays of pointers? in which cases?
    and if .. for example... we have a program that must read an array of any type using pointers? how can i write this program and let it scan the array to find a specific value?
    in this example we dont need arrays of pointers, do we? and ,, i did this but there'z something wrong in it that i can't know wut is it! CAN ANYBODY HELPPPP ..PLZ?!
    code:
    Code:
    # include <iostream.h>
     
      int main(){
    const char  array[]= "Hello world!";
     char  *aptr=array;
     char x;
     cout<<"enter the char. u wish to test  aviliablility, please\n";
     cin>> x;
          for(int i=0;*aptr!='\0'; i++){
       if ( array[i]==x)
        *aptr=x;
     cout<<" found!";
    else 
       ptr++;
    } 
    cout<< " not found!";
     return 0;
    }

  14. #14
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    @ csc_paradise28

    I'm far from an experienced programmer and only am starting to get a grasp, but, maybe this can help.

    And to your question, to find a certain letter, you don't need to use pointers, well, certainly not in this example.
    Code:
    #include <iostream>
    
    using namespace std;
     
    int main()
    {
    	char array[] = "Hello World";
    	char* pArray = array;
    	char myChar, found = 'n';
    
    	cout << "The sentence is: " << pArray << "\n\n";
    	cout << "Wich character do you wish to search?\n";
    
    	cin >> myChar;
    
    	for (int i = 0; i < sizeof array/sizeof *array; i++)
    	{
    		if (*(pArray + i) == myChar)
    			found = 'y';
    	}
    
    	if(found == 'y')
    		cout << "Found it!\n";
    	else
    		cout << "Not found!\n";
    
    	cout << "Press enter to exit!\n";
    
    	return 0;
    }
    And next time, don't hijack someone else his thread, it's not polite
    Last edited by JoBe; 01-09-2006 at 06:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you input data from a file into an array?
    By jorgejags in forum C Programming
    Replies: 5
    Last Post: 11-03-2008, 02:48 PM
  2. reading in file data
    By sickofc in forum C Programming
    Replies: 2
    Last Post: 03-01-2006, 05:16 PM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM