The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Streams, chars and text files
Discuss Streams, chars and text files in the C Programming forum on Dev Shed. Streams, chars and text files C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 18th, 2004, 03:32 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Streams, chars and text files
Hey guys,
This piece of code reads paths from a text file and then tries to open to see if they exist. It should then print the path followed by found or not found:
PHP Code:
char file[100];
string file_list[100];
int element_no = 0;
ifstream def_file("locations.def");
if (!def_file)
{
cerr << "Couldnt open definitions file" << endl;
}
char line;
while (def_file.get(line))
{
file_list[element_no] = line;
cout << file_list[element_no];
element_no++;
}
def_file.close();
int i;
for (i = 0; i < element_no; i++)
{
ifstream infile(file_list[i].c_str());
if (!infile)
{
cout << file_list[i] << "...NOT found";
} else {
cout << file_list[i] << "...File found";
}
}
Somethings going wrong and I get this output:
/...File foundh...NOT foundo...NOT foundm...NOT founde...NOT found/...File foundf...NOT founda...NOT foundd...NOT founde...NOT found/...File foundc...NOT foundp...NOT founda...NOT foundn...NOT found....File founds...NOT foundh...NOT found
...NOT found/...File founde...NOT foundt...NOT foundc...NOT found/...File foundn...NOT foundo...NOT foundt...NOT foundf...NOT foundo...NOT foundu...NOT foundn...NOT foundd...NOT foundn...NOT founde...NOT foundt...NOT foundd...NOT found....File foundc...NOT foundo...NOT foundn...NOT foundf...NOT found
...NOT found
Can someone tell me whats going on, I think its to do with my variable types but I'm new to streams and working with text files so im a little bewildered. How can I fix this?
Thanks
|

August 18th, 2004, 03:43 PM
|
 |
Lord of Dorkness
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
Show a representative sample of your datafile contents. What is there, plus what your documentation tells you (on careful reading) that ifstream::get does probably holds the key.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.
|

August 18th, 2004, 04:00 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Sorry, forgot to post a sample. Its just paths on newlines like:
/home/fade/cpan.sh
/etc/notfoundnetd.conf
(thats the actual 2 lines that gave the above output)
|

August 18th, 2004, 04:06 PM
|
 |
Contributing User
|
|
|
|
Code:
char line;
while (def_file.get(line))
Wouldn't you want a string for the line, and read it with getline?
The output appears to be each character composing the filename followed by the message (you might want to add a '\n' after the message to help make it more obvious).
|

August 18th, 2004, 04:32 PM
|
 |
Lord of Dorkness
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
Precisely. Even if you specify a char array, length, and delimiter with 'get', the \n is gonna cause headaches because it doesn't get extracted.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|