C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 18th, 2004, 03:32 PM
0bdurate 0bdurate is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 0bdurate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 (
0element_noi++)
{
    
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

Reply With Quote
  #2  
Old August 18th, 2004, 03:43 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Lord of Dorkness
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
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.

Reply With Quote
  #3  
Old August 18th, 2004, 04:00 PM
0bdurate 0bdurate is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 0bdurate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #4  
Old August 18th, 2004, 04:06 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
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).

Reply With Quote
  #5  
Old August 18th, 2004, 04:32 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Lord of Dorkness
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Streams, chars and text files

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap