Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,458 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,856 people online right now. Registration is fast and FREE... Join Now!




File input stream

 
Reply to this topicStart new topic

File input stream, Incomplete type error

Techno Mage
3 Aug, 2008 - 08:10 AM
Post #1

D.I.C Head
**

Joined: 10 Jun, 2007
Posts: 58


My Contributions
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:

CODE
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:

CODE

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?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: File Input Stream
3 Aug, 2008 - 08:40 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,210



Thanked: 214 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif

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: 3 Aug, 2008 - 08:42 AM
User is online!Profile CardPM
+Quote Post

Techno Mage
RE: File Input Stream
3 Aug, 2008 - 10:00 AM
Post #3

D.I.C Head
**

Joined: 10 Jun, 2007
Posts: 58


My Contributions
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 sad.gif

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.

CODE

#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: 3 Aug, 2008 - 10:09 AM
User is offlineProfile CardPM
+Quote Post

Hyper_Eye
RE: File Input Stream
3 Aug, 2008 - 10:19 AM
Post #4

D.I.C Head
**

Joined: 13 Sep, 2007
Posts: 72



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

cpp
#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: 3 Aug, 2008 - 10:31 AM
User is offlineProfile CardPM
+Quote Post

Techno Mage
RE: File Input Stream
7 Aug, 2008 - 08:54 AM
Post #5

D.I.C Head
**

Joined: 10 Jun, 2007
Posts: 58


My Contributions
:|

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

CODE

#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?
User is offlineProfile CardPM
+Quote Post

KYA
RE: File Input Stream
7 Aug, 2008 - 08:58 AM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,881



Thanked: 96 times
Dream Kudos: 1200
My Contributions
code=cpp inside the tags

Why not malloc the size of argv+1 to the pointer and go from there?
User is online!Profile CardPM
+Quote Post

Techno Mage
RE: File Input Stream
7 Aug, 2008 - 09:50 AM
Post #7

D.I.C Head
**

Joined: 10 Jun, 2007
Posts: 58


My Contributions
QUOTE(KYA @ 7 Aug, 2008 - 12:58 PM) *

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*?
User is offlineProfile CardPM
+Quote Post

Techno Mage
RE: File Input Stream
9 Aug, 2008 - 05:23 PM
Post #8

D.I.C Head
**

Joined: 10 Jun, 2007
Posts: 58


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

CODE

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):

cpp

#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;
}

User is offlineProfile CardPM
+Quote Post

KYA
RE: File Input Stream
9 Aug, 2008 - 07:17 PM
Post #9

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,881



Thanked: 96 times
Dream Kudos: 1200
My Contributions
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.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 03:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month