For a program i'm writing I need to read a .txt file into a 2-d array. The txt file contains spaces that need to also be read into the array, so I was told I need to use getline, but after searching google and this site i'm not sure exactly how to use it. The 1st and last rows of the array also need to be blank, which is why i've started the for loops at 1. Any help would be much appreciated!

void readfile (string filename, char ba[][MAXCOL])
{
	int inputline = 80;
	ifstream myfile;
	myfile.open("blob.txt");
	for (int row=1; row<(MAXROW - 1); row++)
	{
		//getline
		for (int col=1; col<(MAXCOL - 1); col++)
		{
			//getline?
			
		}
	}
	myfile.close();
}

Syntax for getline

getline(INSTREAM, HOLDING VARIABLE, DELIM);

Your instream would be your myfile, your holding variable is your variable that you store the line into, and the delim is the character that getline stops reading at.

Thanks for the response, but is there something else i need to do? I'm getting these errors using that syntax. And line 56 of my code is the line with getline.

1>c:\users\kusel\documents\visual studio 2010\projects\assignment 7\assignment 7\blob.cpp(56): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1> c:\program files\microsoft visual studio 10.0\vc\include\string(479) : see declaration of 'std::getline'
1>c:\users\kusel\documents\visual studio 2010\projects\assignment 7\assignment 7\blob.cpp(56): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
1> c:\program files\microsoft visual studio 10.0\vc\include\string(468) : see declaration of 'std::getline'
1>c:\users\kusel\documents\visual studio 2010\projects\assignment 7\assignment 7\blob.cpp(56): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'int'
1> c:\program files\microsoft visual studio 10.0\vc\include\string(468) : see declaration of 'std::getline'
1>c:\users\kusel\documents\visual studio 2010\projects\assignment 7\assignment 7\blob.cpp(56): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1> c:\program files\microsoft visual studio 10.0\vc\include\string(448) : see declaration of 'std::getline'
1>c:\users\kusel\documents\visual studio 2010\projects\assignment 7\assignment 7\blob.cpp(56): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'std::ifstream'
1> c:\program files\microsoft visual studio 10.0\vc\include\string(395) : see declaration of 'std::getline'
1>c:\users\kusel\documents\visual studio 2010\projects\assignment 7\assignment 7\blob.cpp(56): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'int'
1> c:\program files\microsoft visual studio 10.0\vc\include\string(395) : see declaration of 'std::getline'

string lines[69];

int i=1;
while(getline(infile, lines[i]))
{
     i++;
}

for the program im writing this is what i have to do:

i have to write a C program that maintains a table of productID and price in a .txt file.

The following menu shows the functionality of the program:
1. Display all products
2. Search product by ID
3. Sort the product list by price
4. Sort the product list by ID
5. List the average price
6. List max and min price
7. Exit

Specifications:
1. The product IDs and product prices are stored in a table format in a file named
products.txt. Create a text file in the same folder as your .c file and type in two columns
(ID and price) separated by a tab. product ID and price are integers
2. Main program will be main.c. Define the following functions in func.h and func.c files
(refer to lab 5, question 1).
 displayMenu( ): displays the menu options 1. -7. on the screen, and asks the user
to ‘Enter an option:’.
 readFile ( ) – opens the file ‘products.txt’ and reads contents from the file is read
in to a 2-D array named ‘products’.
 displayProducts ( ) – displays the contents of the products list
 searchProduct( ) – searches for a product by its ID and displays whether it is
found or not.
 sortProductsPrice ( ) – sorts the products list in ascending order of the price, and
displays the sorted list
 sortProductsID ( ) – sorts the products list in ascending order of the ID, and
displays the sorted list
 averagePrice ( ) – calculates and displays the average price
 maxMinPrice( ) – calculates the maximum and minimum price in the list and
displays the max and min values

so far i have this i dont know how to take input from a .text file helpp!!

#include <stdio.h>
#include <stdlib.h>

void displayMenu();
void readFile();
void displayProducts();
void searchProduct();
void sortProductsPrice();
void sortProductsID();
void averagePrice();
void maxMinPrice();

int main(int argc, char *argv[])
{   
    int userChoice = 0;
    printf("Inventory Program\n\n");
    displayMenu();    

     printf(" \nPick an option\n");
    scanf("%d",&userChoice); 

     switch(userChoice)
     {
        case 1: displayProducts(); 
        break;
        case 2: readFile();  
        break;               
        case 3: searchProduct();     
        break; 
        case 4: sortProductsPrice();
        break; 
        case 5: sortProductsID();   
        break; 
        case 6: averagePrice();      
        break; 
        case 7: maxMinPrice();       
        break; 
    }

  system("PAUSE");  
  return 0;
}
void displayMenu()
{    
       printf("1 To Display Products Enter 1\n");
       printf("2 To Extract Products From Txt File Enter 2\n");
       printf("3 Search By  Product I.D Enter 3 \n");
       printf("4 Search And Sort Product By Prices 4\n");
       printf("5 Search And Sort By  Product I.D Enter 5\n");
       printf("6 Search Average Price Enter 6\n");
       printf("7 Calculate Maximum And Minimum Prices And Displays Them 7\n");

}
void readFile()
{

}
void displayProducts()
{

}
void searchProduct()
{

}
void sortProductsPrice()
{

}
void sortProductsID()
{

}
void averagePrice()
{

}
void maxMinPrice()
{

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.