Basic, I'm trying to read in a file that has lines of data, where each set is on a new line, and each variable is separated by whitespace, save for the name, which can have multiple white spaces. Here's basically my code, the parts that matter.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>

std::ifstream inputFile;	// Simple
inputFile.open("data.txt", std::ios::in);

while(i < MAXENTRIES && !inputFile.eof() && inputFile.good())
{
     inputFile >> variable1;
     inputFile >> variable2;
     inputFile >> variable3;
     inputFile >> variable4;
     inputFile >> variable5;
     // etc...
}

The loop works fine, it goes through twenty times, which is the maximum amount. However, nothing is read. I can do a quick printf on any variable immediately after it is "read," but they contain nothing. Any error checking I try comes up with nothing, so the loop still ones. What I really don't get is that the file read perfectly fine using C code (fscanf).

Any ideas?

>>The loop works fine
No it doesn't. That loop may read the last line of the file twice, assuming there are fewer than MAXENTRIES number of lines in the file.

My guess is that you have just dummied up the code you posted without bothering to copy/paste from the code you really have on your compiler. Just code is nearly useless to us. If you expect help then post the actual code you have, not something that is just contrived.

This is the full code of the function. I didn't post it first as it is fairly large. Ignore any errors that aren't directly related the data in the file being read in, as I haven't gotten around to fixing that.

int openDataFile(activity activityData[])
{
	std::ifstream inputFile;	// Simple
	inputFile.open("data.txt", std::ios::in);
	int reading = 1;	// Active Loop
	char f[20];
	int i = 0;	// Counter
	int a = 0;
	int o = 0;	// Count
	int bytes = 200;
	char check;	// Error checking
	int hcheck;	// Error Checking
	int mcheck;	// Error Checking
	char * namestr;	// Name Check
	char * namecheck;
	char buildingstr[BUILDINGLENGTH];	// Building Check
	char roomstr[ROOMLENGTH];	// Room check 
	float totalcost = 0;	// Total cost of all groups
	int totalsize = 0;		// Total size of all groups
	int tempID = 0;
	int tempSize = 0;
	int tempWeek = 0;
	int tempHour = 0;
	int tempMinute = 0;
	int tempNMonth = 0;
	int tempNDay = 0;
	int tempLength = 0;
	int tempEnum = 0;
	char tempDay;
	char tempPurpose;
	char tempMember;
	char tempMeridiem;
	char tempBuffer;
	char tempName[(NAMELENGTH+1)];
	char tempBuilding[(BUILDINGLENGTH+1)];
	char tempRoom[(ROOMLENGTH+1)];
	float tempCost;
	float tempTime;
	union local tempUnion;
	
	// Easy enough ;)
	printf("WARNING: Continuing may cause any\npresent data to be overwritten.\n\nContinue? (y/n): ");
	
	// Get input that is letters or numbers, but only one character long
	scanf("%1[0987654321abcdefghijklmnopqrstuvwxyz]hc%*c", &check);
	
	// Flush the input buffer
	flushBuffer();
	
	// Easy
	namestr = (char *)malloc(bytes+1);
	
	// Copy string
	for(a = 0; a < BUILDINGLENGTH; a++)
	{ buildingstr[a] = ' '; }
	
	// Copy string
	for(a = 0; a < ROOMLENGTH; a++)
	{ roomstr[a] = ' '; }

	// If they enter 'q', set the program to terminate
	if(check == 'y')
	{
		//flushBuffer();
		
		printf("\n\n----------------------------------------\n");
		
		printf("Reading file: data.txt ...\n\n");
		
		// Table Header
		printHeader();
		
		// Loop through the data in the file
		while (i < MAXENTRIES && !inputFile.eof() && inputFile.good())
		{						
			// ID, Hour, Min, M, Day, Week, Purpose, Eligibiltity, Size, Cost
			// Fix this so it can't load more than one character
			// if (fscanf(inputFile, "%d %d %d %d.%d %c %c %d%1s%d %c %c %s %s %f %d %d", &activityTemp->ID, &activityTemp->Size,
			// &activityTemp->Week, &hcheck, &mcheck, &activityTemp->Meridiem, &activityTemp->Day, &activityTemp->NMonth, f, &activityTemp->NDay,
			// &activityTemp->Purpose, &activityTemp->Member, buildingstr, roomstr, &activityTemp->Cost, &activityTemp->Length, &x) != 17)
			// {
			// 	reading = 0;
			//	//free(activityTemp);
			// }
			
			inputFile >> tempID;
			inputFile >> tempSize;
			inputFile >> tempWeek;
			inputFile >> tempHour;
			inputFile >> tempMinute;
			inputFile >> tempMeridiem;
			inputFile >> tempDay;
			inputFile >> tempNMonth;
			inputFile >> tempBuffer;	// Not used, just absorbs the /
			inputFile >> tempNDay;
			inputFile >> tempPurpose;
			inputFile >> tempMember;
			inputFile >> tempBuilding;
			inputFile >> tempRoom;
			inputFile >> tempCost;
			inputFile >> tempLength;
			inputFile >> tempEnum;
			
			printf("%d", tempID);

			if(reading != 0)
			{
				// Cast x into an enumerated type
				if(tempEnum < 4 && tempEnum >= 0)
					activityData[i].set_Affiliation((enum national)tempEnum);
				else
					activityData[i].set_Affiliation((enum national)(rand()%4));
					
				// Depending on what type of value in affiliation, we use a different part of extra
				switch(activityData[i].get_Affiliation())
				{
					case NONE:	// NONE
						inputFile >> tempUnion.year;
						activityData[i].set_Extra(tempUnion);
						break;
					case STDT:	// STDT
						inputFile >> tempUnion.stdtdues;
						activityData[i].set_Extra(tempUnion);
						break;
					case UNIV:	// UNIV
						//inputFile >> tempUnion.school;
						activityData[i].set_Extra(tempUnion);
						break;
					case REG:	// REG
						inputFile >> tempUnion.subgroup;
						activityData[i].set_Extra(tempUnion);
						break;
					default:
						break;
				}
		
				// Get the last part, the name, and the terminating character with getline
				//inputFile.getline(tempName, NAMELENGTH);
				
				// If AM/PM is not valid, throw an error
				if(tempMeridiem != 'p' && tempMeridiem != 'P' && tempMeridiem != 'a' && tempMeridiem != 'A')
				{
					tempMeridiem = 'A';
				}
					
				// If Member Type is not valid, throw an error
				if(tempMember != 'A' && tempMember != 'G' && tempMember != 'M' && tempMember != 'P' && tempMember != 'U' && tempMember != '3' && tempMember != '2' &&
					tempMember != '4' && tempMember != 'a' && tempMember != 'g' && tempMember != 'm' && tempMember != 'p' && tempMember != 'u')
				{
					tempMember = 'A';
				}
				
				// If Purpose is not valid, throw an error
				if(tempPurpose != 'F' && tempPurpose != 'V' && tempPurpose != 'A' && tempPurpose != 'S' && tempPurpose != 'P' && tempPurpose != 'M' && tempPurpose != 'L' && tempPurpose != 'C' && tempPurpose != 'O' &&
					tempPurpose != 'f' && tempPurpose != 'v' && tempPurpose != 'a' && tempPurpose != 's' && tempPurpose != 'p' && tempPurpose != 'm' && tempPurpose != 'l' && tempPurpose != 'c' && tempPurpose != 'o')
				{
					tempPurpose = 'F';
				}
				
				// If Day is not valid, throw an error
				if(tempDay != 'M' && tempDay != 'T' && tempDay != 'W' && tempDay != 'R' && tempDay != 'F' && tempDay != 'S' && tempDay != 'N' &&
					tempDay != 'm' && tempDay != 't' && tempDay != 'w' && tempDay != 'r' && tempDay != 'f' && tempDay != 's' && tempDay != 'n')
				{
					tempDay = 'M';
				}
				
				// Size
				if(tempSize < 0)
				{
					tempSize *= -1;
				}
				
				// Check Hour
				if(hcheck <= 12 && hcheck > 0 && mcheck >= 0 && mcheck < 60)
				{
					tempTime = (float)mcheck / 100;
					tempTime += hcheck;
				}
				else
				{
					tempTime = 12.00;
				}
				
				// Check next day
				if(tempNDay < 1)
					tempNDay = 1;
				if(tempNDay > 31)
					tempNDay = 31;
					
				// Check next month
				if(tempNMonth < 1)
					tempNMonth = 1;
				if(tempNMonth > 12)
					tempNMonth = 12;
				
				// Copy string
				//strncpy(tempName, namestr, NAMELENGTH);
				tempName[strlen(tempName)] = '\0';
				
				// Copy string
				//strcpy(tempBuilding, buildingstr, BUILDINGLENGTH);
				tempBuilding[strlen(tempBuilding)] = '\0';
			
				// Copy string
				//strcpy(tempRoom, roomstr, ROOMLENGTH);
				tempRoom[strlen(tempRoom)] = '\0';
				
				// If ID is not valid, set it to zero, cancelling out the entry
				if(tempID > 999 || tempID < 1)
				{
					activityData[i].set_ID(0);
					activityData[i].set_Time(0.00);
					activityData[i].set_Meridiem('P');
					activityData[i].set_Day('M');
					activityData[i].set_Week(1);
					activityData[i].set_Purpose('F');
					activityData[i].set_Member('A');
					activityData[i].set_Size(0);
					activityData[i].set_Cost(0);
					activityData[i].set_Length(0);
					activityData[i].set_NDay(0);
					activityData[i].set_NMonth(0);
					activityData[i].set_Affiliation((enum national)0);
					
					tempUnion.year = 0;
					activityData[i].set_Extra(tempUnion);
				}
				else
				{
					activityData[i].set_ID(tempID);
					activityData[i].set_Time(tempTime);
					activityData[i].set_Meridiem(tempMeridiem);
					activityData[i].set_Day(tempDay);
					activityData[i].set_Week(tempWeek);
					activityData[i].set_Purpose(tempPurpose);
					activityData[i].set_Member(tempMember);
					activityData[i].set_Size(tempSize);
					activityData[i].set_Cost(tempCost);
					activityData[i].set_Length(tempLength);
					activityData[i].set_Affiliation((enum national)tempEnum);
					activityData[i].set_Extra(tempUnion);
					activityData[i].set_NDay(tempNDay);
					activityData[i].set_NMonth(tempNMonth);
				}
				
				// Print the data as we load it, just to check to make sure it is
				// being read correctly. Mostly for debuggin purposes
				displayLine(i, activityData);
				
				mcheck = 0;
				hcheck = 0;
				
				// Copy string
				for(a = 0; a < NAMELENGTH; a++)
				{ namestr[a] = ' '; }
				
				// Copy string
				for(a = 0; a < BUILDINGLENGTH; a++)
				{ buildingstr[a] = ' '; }
				
				// Copy string
				for(a = 0; a < ROOMLENGTH; a++)
				{ roomstr[a] = ' '; }
				
				//free(namestr);
				
				// Calculate totals
				totalcost += activityData[i].get_Cost();
				totalsize += activityData[i].get_Size();
				
				i++;
			}
		}
		
		printf("\nRead %d entries\n", i);
		
		// Print totals
		printf("Total Cost: $%.2f\n", totalcost);
		printf("Average Cost: $%.2f\n", (totalcost/i));
		printf("Total Size: %d\n", totalsize);
		
		// Print a divider. Just looks a little nicer.
		
		std::cout << "----------------------------------------\n";
		
		inputFile.close();
		
		return MENUMAIN;
	}
	else
	{
		std::cout << "\nFile was NOT read.\n";
		
		// Print a divider. Just looks a little nicer.
		
		std::cout << "----------------------------------------\n";
		
		inputFile.close();
		
		return MENUINPUT;
	}
}

The only reason I can see why that does not read the file is when inputFile failes to open. Add a check right after line 4 to verify the open succeeded.

if( !inputFile.is_open() )
{
    printf("Open failed\n"');
    return 0;
}

The only reason I can see why that does not read the file is when inputFile failes to open. Add a check right after line 4 to verify the open succeeded.

if( !inputFile.is_open() )
{
    printf("Open failed\n"');
    return 0;
}

Still runs through the loop without reading anything, so the file is open, just not reading. Is it possible that perhaps the file pointer is somewhere in the file that isn't the beginning?

can you post the first few lines of the data file?

132 53 1 12.00 p M 11/8 P E NH 110 15.00 60 1 8.00 Soc. Women Engineers      
512 120 1 1.30 p M 10/29 M A MB 103 40.00 45 2 UTA Concert Choir             
135 75 1 10.00 a W 12/13 P E NH B024 55.00 30 1 10.00 TX Soc. Prof. Engineers   
141 30 4 10.00 a S 11/4 P A NH 315 50.00 60 0 2008 Game Developer's Club    
134 47 4 4.00 p R 10/30 F A UC 100 5.00 180 2 UTA Bed Races                
491 156 2 8.00 p T 10/30 V A UC B064 9.00 180 2 UTA UTA Volunteers           
140 25 1 11.00 a W 10/30 P E NH 315 15.00 25 3 36 ACM                      
130 20 1 12.00 p M 11/13 A E NH 100 10.00 20 1 15.00 JCEO Eng. Student Council
150 55 2 4.00 p T 11/3 P E NH 142 10.00 40 1 20.00 IEEE - I Triple EEE      
488 15 3 1.00 p W 11/10 L A UC B100 10.00 35 2 UTA Student Senate           
610 95 4 10.30 p M 12/2 C A PH 110 20.00 20 1 5.00 BSA                      
171 14 2 12.00 p T 12/5 P G ELB 117 30.00 30 0 2009 BioMedical Engineering   
490 58 3 12.30 p W 12/10 V A UC 212A 50.00 15 3 25 FLOC                     
008 120 3 6.00 p F 11/9 S A MAC VB1 15.00 120 2 UTA Oozeball

Probably because the data types of the data file are not the same as in your program. The 4th number on the first line is a float, not an integer, and the next two are characters, not integers. ifstream will produce an error when attempting to read a char into an int variable, and all subsequent input operations will fail. The same problem when attempting to convert a float to an int -- ifstream doesn't know what to do with that deccimal point.

I'm trying to read in the decimal value as two integers, as one represents an hour time and the other represents a minute time, that way I can check for ranges later (1-12, 0-60). Is there anyway to do this? Also, would that error purge what has already been read? I try to print the variable tempID right after I read it in and it still renders as 0.

one way to do that is to read it as if it were three different variables

char period;
...
			inputFile >> tempHour >> period >> tempMinute;

This is what I have now in by reading area, I included a second buffer read to hopefully absorb the period. Still no luck.

inputFile >> tempID;
inputFile >> tempSize;
inputFile >> tempWeek;
inputFile >> tempHour;
inputFile >> tempBuffer;
inputFile >> tempMinute;
inputFile >> tempMeridiem;
inputFile >> tempDay;
inputFile >> tempNMonth;
inputFile >> tempBuffer; // Not used, just absorbs the /
inputFile >> tempNDay;
inputFile >> tempPurpose;
inputFile >> tempMember;
inputFile >> tempBuilding;
inputFile >> tempRoom;
inputFile >> tempCost;
inputFile >> tempLength;
inputFile >> tempEnum;

There is a similar problem with that p M that preceeds the month/year. You need to re-check each one of those fields in the data file and make sure your program reads corresponding variables.

Okay, just to throw another wrench in the gears, if I compile this on its target platform (Schools UNIX server), it works fine, just not using my local compiler.

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.