File input stream

Incomplete type error

Page 1 of 1

8 Replies - 13869 Views - Last Post: 09 August 2008 - 08:17 PM Rate Topic: -----

#1 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

File input stream

Posted 03 August 2008 - 09:10 AM

First off, I have searched for this. Honest. I can't find anything on google for it.

Second, do you guys know a good reference for standard header files and the C++ standard library?

Here's the real question. I'm just beginning in C++ and I'm trying to write a program that will take text files and cut them up into smaller pieces around 4kb in size for the iPod. I've tried it in Java and it's ugly. Now, I have declared an input file stream (ifstream inFile) but when I compile what I have so far, I get this error:

 error: aggregate ‘std::ifstream inFile’ has incomplete type and cannot be defined



I don't know why I'm getting this and I'm following the "Opening a file" section of this tutorial.

Thanks guys.

PS: This is I have a question about int main(). Now, I know you guys might laugh at this selection, but I've just finished C++ for Dummies. Almost every declaration of main is like this:

int main(int nNumberofArgs, char* pszArgs[])



What's the reason of passing in a pointer at main rather than just a variable?
If char* was declared as char&, that makes it a reference, right?

Is This A Good Question/Topic? 0
  • +

Replies To: File input stream

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: File input stream

Posted 03 August 2008 - 09:40 AM

Well you typically see that error when you are attempting to use a structure or file pointer that has not been defined. So we will need to see your code (please put in code tags) and then we can see what you have defined and haven't defined.

As for your second question, the reason it is a pointer being passed to main is that the array is an array to variable length character arrays (aka strings). Since the parameters coming in are unknown length and can vary from parameter to parameter, you need a pointer to those strings and not the strings themselves. The program has no way of knowing how long these strings are going to be so it can't set aside memory for them etc.

I hope that makes sense. :)

P.S. Yes the & makes them references, but you don't want references in this case. You want the addresses to the strings (the pointers).

This post has been edited by Martyr2: 03 August 2008 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File input stream

Posted 03 August 2008 - 11:00 AM

Thanks, but as it turns out, I didn't put the include file for fstream. It's all better now, except that I'm getting a segmentation fault error :(

Here's my code. I feel it's a bit sloppy but I can improve on it later. Let me know if you think there's anything I should change in my exception handling, as this is my first time really trying it.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sys/stat.h>
#include <string>
#include <fstream>

using namespace std;


struct stat fileResults;

//Tests if the file exists
bool fileExists(string fName)
{
	if(stat(fName.c_str(),&fileResults)==0)
		return true;
	else
		return false;
}

	
	
int main(string input)
{
	string fileName = input;
	long size = 0;
	ifstream inFile;
	//Checks if anything was entered through the terminal
	//Or dragged from an icon
	try
	{
		if(input.c_str()!=NULL)
			if(fileExists(fileName)==true)
				inFile.open(fileName.c_str());
			else
				throw string("File does not exist.");
		else
		{
			cout << "Enter the name of the file:\n";
			cin >> fileName;
			if(fileExists(fileName)==true)
				inFile.open(fileName.c_str());
			else
				throw string("File does not exist.");
		}
	}
	
	catch(string error)
	{
		cout << "Error:" << error;
	}
	
	size = fileResults.st_size;
	cout << size;
}



Using a string as an argument to main is fine right?

This post has been edited by Techno Mage: 03 August 2008 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: File input stream

Posted 03 August 2008 - 11:19 AM

You pointed out in your first post what the declaration of main() is. You can use it to get your string:

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    string input;

    if(argc < 2)
        return -1;

    input = argv[1];

    cout << input << endl;

    return 0;
}

This post has been edited by Hyper_Eye: 03 August 2008 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#5 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File input stream

Posted 07 August 2008 - 09:54 AM

:|

Well guys, I've fixed my problems thus far by going back and studying pointers some more so I can at least get the file size of something that I've entered through command line. However, I want this program to accept input from command line. If there is no input via command line, the user should be asked for input. I got this done correctly except for one problem: Segfault. I'm pretty sure I'm getting this problem because if it asks for input with cin, it's doesn't add a null character to the end of the string since I'm using a char pointer. I'm not sure how I could mix something like string with my code and I don't know how I'd add a null character at the end of this.

Here's my code again

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sys/stat.h>
#include <string>
#include <fstream>

using namespace std;


struct stat fileResults;

//Tests if the file exists
bool fileExists(char fName[])
{
	//fileResults = new stat(fName.c_str(),&fileResults);
	if(stat(fName,&fileResults)==0)
		return true;
	else
		return false;
}

	
	
int main(int argc, char* argv[])
{
	//Checks to see if there is was an argument through commandline
	char* fileName;
	if(argc>1)
		fileName = *(argv+1);
	long size = 0;
	ifstream inFile;
	//Checks if anything was entered through the terminal
	//Or dragged from an icon
	try
	{
		if(argc>1)
			if(fileExists(fileName)==true)
				inFile.open(fileName);
			else
				throw string("File does not exist.\n");
		else
		{
			cout << "Enter the name of the file:\n";
			cin >> fileName;
			
			if(fileExists(fileName)==true)
				inFile.open(fileName);
			else
				throw string("File does not exist.\n");
		}
	}
	
	catch(string error)
	{
		cout << "Error: " << error;
	}
	
	size = fileResults.st_size;
	cout << size;
	return 0;
}



PS: How'd you do the C++ tags?
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: File input stream

Posted 07 August 2008 - 09:58 AM

code=cpp inside the tags

Why not malloc the size of argv+1 to the pointer and go from there?
Was This Post Helpful? 0
  • +
  • -

#7 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File input stream

Posted 07 August 2008 - 10:50 AM

View PostKYA, on 7 Aug, 2008 - 12:58 PM, said:

code=cpp inside the tags

Why not malloc the size of argv+1 to the pointer and go from there?


I'm going to look that up but a quick question. Is declaring an array of a size essentially malloc and a pointer in one?

Also, is it better to do string, char[], or char*?
Was This Post Helpful? 0
  • +
  • -

#8 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File input stream

Posted 09 August 2008 - 06:23 PM

Okay, I've got it basically done with just 4 errors. I have no idea what they mean or what to do.

C++ Code/podulator.cpp:23: error: variable or field ‘fileCutter’ declared void
C++ Code/podulator.cpp:23: error: expected primary-expression before ‘input’
C++ Code/podulator.cpp:23: error: expected primary-expression before ‘name’



Here's my code (hopefully one last time):

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sys/stat.h>
#include <string>
#include <fstream>
//#include <direct.h>

using namespace std;


struct stat fileResults;

//Tests if the file exists
bool fileExists(string fName)
{
	if(stat(fName.c_str(),&fileResults)==0)
		return true;
	else
		return false;
}

void fileCutter(*ifstream input, string name)
{
	int charCount = 0;
	int fNum = 1;
	string fstr;
	ofstream outFile;
	//Writes the files
	while(input.eof()==false)
	{
		//Sets string to null
		fstr=NULL;
		//Places 3850 characters in a string to be written to file
		//while 3850 hasn't been reached or the end of the file
		do
		{
			
			strcat(fstr, input.get());
			charCount++;
		}while(charCount%3850!=0 && input.eof()==false);
		outFile.open(name+"part_"+fnum+".txt", ios::trunc);
		outfile << fstr;
		outFile.close();
		fNum++;
	}
	return;
}

	
int main(int argc, char* argv[])
{
	//Checks to see if there is was an argument through commandline
	string fileName;
	if(argc>1)
		fileName = (string)*(argv+1);
	long size = 0;
	ifstream inFile;
	int fileCount = 0;
	//Opens file depending on if there's input from terminal or not.
	try
	{
		if(argc>1)
			if(fileExists(fileName)==true)
			{
				//Outputs size of file and opens it
				inFile.open(fileName.c_str());
				size = fileResults.st_size;
				cout << size<<endl;
			}
			else
				throw string("File does not exist.\n");
		else
		{
			cout << "Enter the name of the file:\n";
			//Accepts input for fileName but also allocates 1 more space
			//for a null character
			
			cin >> fileName;
			
			if(fileExists(fileName)==true)
			{
				//Outputs size of file and opens it
				//While throwing an exception if we can't open the file
				inFile.open(fileName.c_str());
				if(inFile.bad() == true)
					throw string("Cannot open file.");
				size = fileResults.st_size;
				cout << size<<endl;
			}
			else
				throw string("File does not exist.\n");
		}
		//If fileName really is a file but not a text file
		//throw an error.
		if(strcasestr(fileName.c_str(), ".txt")==NULL)
			throw string("Not a text file.\n");
		//Make a directory with the name of the file being read
		mkdir("/home/tetrix/Documents/"+fileName);
		fileCutter(inFile,"/home/tetrix/Documents/"+fileName+"/");
		cout << "File cut!";
	}
	
	catch(string error)
	{
		cout << "Error: " << error;
	}
	catch(bad_alloc mError)
	{
		cout <<  "Memory Allocation error";
	}
	//Prints size of file (for testing)
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: File input stream

Posted 09 August 2008 - 08:17 PM

If you have a parameter that is a pointer to ifstream the '*' needs to be on the other side of the type name, i.e. in this case ifstream.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1