ZONE
FEATURES
Home
News
Check Email
LEARNING ZONE
C++ Tutorial
Tutorials collection
Ask the Expert
FAQs
Refferences
ARTICLES
All articles
DOWNLOADS
Compilers
Sourcode Library
Tools/Utilities
WINDOWS
Win programing
C++ Builder
UNIX
Unix programming
Unix downloads
WEB PROGRAMMING
CGI programming
CGI Scripts
RESOURCES
Magazines
Newsletters
Mailing Lists
Courses
Links
COMMUNICATION
Message board
Chat
Communities
Organisations
C++ Groups
C++ Newsgroups
ABOUT US
Contact us
submit a resource
Privacy
Link to us
 
 
 
Home Ask the expert printf
 

Ask me a question

Main Q & A Page
 
SUBJECT : Why dont this program work?

A textfile( "test" ) is supposed to replace the textbuffer(matrix[30][100] ) and then print the text from the buffer. i'll be very happy if You could help a beginner like me in c++.*/


#include <iostream> 
#include <fstream> 

int i=0, j=0; 
main(){ 

char matrix[30][100]; 
char c; 

ifstream f1("test", ios::in); 
while(f1.get(c)) 
 {  
  matrix[i][j]=c; 
  flush(c); 
  j++; 
  if(c=='\n'){ 
  i++; 
  j=0; 
 } 
} 

for(int i=0; i<matrix[i][40]&matrix[i][40]!='\n'; i++) { 
 cout><matrix[i]><endl; 
} 
cout><"number of rows: "<<i<<endl; 

cin>>ws; 
}

 

     
Sender : Jonathan Riddell  
 
Reply1

First of all, I assume you're aware that you are limiting yourself to files with <=100 characters per line and <=30 lines. It isn't a problem to increase these values, but I'd at least put in a check to make sure you don't overflow the buffer (for instance, you assume that somewhere before the 30-characters-per-line limit, there is a newline - make sure i doesn't go above 30 and j doesn't go above 100!).

If you want to avoid the whole issue of potential overflows, try using some of the data structures in the Standard Template Library (STL). Some have dynamic memory allocation built-in, so you can just keep adding data without worrying about the size. SGI has a pretty good web page at http://www.sgi.com/tech/stl/ which should give you an idea of the various data structures and methods available. Off the top of my head, I think vectors are similar to arrays, but they can be dynamically expanded. You can also have vectors of vectors (ie. two dimensional arrays). There may be others more suited to what you need, so don't take my word for it!

Other points:

* I don't know why you're using flush on a character - it wouldn't even compile on my system, it's a method of the stream (ie. stream.flush() ), and usually used to get stuff out of the stream buffer immediately. For instance, if you are asking the user a question, the text needs to be output straight away, not the next time the buffer is full - the buffer won't get full until *after* the user has input some data and your program has tried to print some more text. Sending the 'endl' token is the same as a newline followed by a flush, I think, but usually a newline tacked onto the end of a sentence does the job. Delete that line.
* Make sure you have opened the file before you try reading the contents (f1 will be NULL if you can't).
* In the output routine, I'm not sure where to begin...
The condition "i<matrix[i][40]&matrix[i][40]!='\n'" is way off. What I assume you're trying to say is "print each of the 30 lines up until the newline, or 100th character, whichever comes first". Your code above has three parts:
1)i must be less than the value of the 41st element in each column
2)the value of the 41st element in each column must not be a newline
3)the overall value of the expression is the bitwise AND of these two, not the logical AND which is what I assume you meant. The bitwise AND will be 1 if both subexpressions are true or 0 otherwise. The subexpressions evaluate to a boolean (ie. true or false, 0 or 1), so even if the subexpressions were correct, your loop would still be doing some weird stuff...

Try something like this instead:

                                                            

for (i=0;i<30;i++) { // For each row (0...29)... for (j=0; j<100; j++) { // ...print all column elements (0...99) cout << matrix[i][j]; if (matrix[i][j]=='\n') { break; // Done printing this row
}
}
}

Bear in mind this will print the whole buffer, so if the file you read in is less than 3K, it will print garbage at the end. You could zero off the last element in the array when you're reading the file, and check before outputting each element whether it's 0 or not- if it is, skip to the next important piece of code...

Hope this helps!

 

Sender : D.O  
 
 
 
 

Ask me a question

Main Q & A Page
 
 
A
Email Login
Password
New?
sign up!
 
A
Free Online tutorial & courses
 
A
Join Our Message board Discussions on C/C++
 
A
Unix Network
programming
tutorial
 
 Join Our Mailing list