Code has nothing I can find wrong, but still won't work.

I am writing a program that reads directory data from a file and it won't work but I can't figure out whats wrong with it. Any help?


// start of code

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct file_temp
{
string date;
string time;
string size;
string filename;
file_temp *next;
};

int main ()
{
string line;
string file;
string check;
file_temp head_file;
file_temp *temp_file;
temp_file = &head_file;
ifstream myfile ("dir.txt");

if (myfile.is_open())
{
check = "M";

while (check == "M")
{
myfile >> temp_file->date;
myfile >> temp_file->time;
myfile >> line;
temp_file->time = temp_file->time + line;
check = temp_file->time[(temp_file->time.length()) - 1];
myfile >> temp_file->size;
getline (myfile,temp_file->filename);
cout << temp_file->date << endl << temp_file->time << endl;
cout << temp_file->size << endl << temp_file->filename << endl;
temp_file = temp_file->next;

}
cout << "\n\n";
myfile.close();

}
else
{
cout << "Unable to open file";
}
system("pause");
return EXIT_SUCCESS;
}

// end of code

----- contents of dir.txt -----

12/26/2008 10:15 AM <DIR> .
12/26/2008 10:15 AM <DIR> ..
12/10/2008 11:11 AM 745 UNZIP.cpp
12/10/2008 11:07 AM 830 UNZIP.dev
01/29/2009 05:43 PM 928 Makefile.win
12/10/2008 11:11 AM 4,469 UNZIP.o
12/10/2008 11:11 AM 501,002 CONFIGURATION.exe
12/16/2008 11:30 AM 168 screech.cpp
12/16/2008 11:29 AM 834 screech.dev
12/16/2008 11:30 AM 1,803 screech.o
01/06/2009 03:31 PM 3,563 winnie.cpp
12/17/2008 11:20 AM 513 DECRYPT.cpp
12/17/2008 11:21 AM 834 XOR_ENC.dev
12/17/2008 10:52 AM 3,224 DECRYPT.o
01/06/2009 02:32 PM 20,360 winnie.exe
01/06/2009 03:32 PM 110 winnie.layout
01/06/2009 03:31 PM 832 winnie.dev
01/06/2009 03:32 PM 1,530 winnie.o
01/09/2009 04:11 PM 175 main.cpp
01/09/2009 04:11 PM 832 event_ex.dev
01/09/2009 04:11 PM 1,811 main.o
01/09/2009 04:11 PM 475,166 event_ex.exe
01/21/2009 03:45 PM 197 winsock_test.cpp
01/21/2009 03:45 PM 844 winsock_test.dev
01/21/2009 03:45 PM 1,811 winsock_test.o
01/21/2009 03:45 PM 475,166 winsock_test.exe
01/21/2009 03:46 PM 96 winsock_test.layout
01/22/2009 10:59 AM 3,344 winsock_client_test.cpp
01/22/2009 10:59 AM 858 winsock_client_test.dev
01/22/2009 10:59 AM 2,727 winsock_client_test.o
01/23/2009 11:09 AM 3,740 window.cpp
01/23/2009 10:46 AM 832 window.dev
01/23/2009 11:09 AM 1,810 window.o
01/23/2009 11:09 AM 21,265 window.exe
01/26/2009 10:47 AM <DIR> WINAPI
01/27/2009 12:04 PM <DIR> custom_cmd
01/29/2009 05:43 PM 431 maindir read test.cpp
01/29/2009 05:30 PM 850 dir read test.dev
01/29/2009 05:43 PM 0 dir.txt
01/29/2009 05:43 PM 3,602 maindir read test.o
01/29/2009 05:43 PM 475,736 dir read test.exe
41 File(s) 2,503,580 bytes
5 Dir(s) 119,079,960,576 bytes free

----- end of dir.txt -----
Last edited on
temp_file = temp_file->next;
The first time, temp_file->next is not pointing to anything valid. Do this first: temp_file->next=new file_temp;
You should add a constructor that initializes next to 0, so you can know where the list ends, and a destructor that automatically frees memory.

Also, a vector would have been better for this.
Thanks, I never would have done that, I was under the impression that simply declaring a pointer to the next item would still be valid.
Topic archived. No new replies allowed.