Thread: formated output without structs or classes

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Stockholm, Sweden
    Posts
    20

    formated output without structs or classes

    I am using Linux and Curses
    As I can remember - In Dos there is a possibility to write to a location of the screens memory.

    Maybe i could use fwrite to a fixed position of stdout.?

    What I want to do is to write formatted output (a long stringarray) which containing some line feeds).

    Is there some way to put them on screen at a fixed point so not the line feeds goes to the next line at column 0?

    If possibile i want to avoid substrate the strings from eachother, and also - i would like to NOT loop through each substring. (i know how to do that - i have some rutines in the program i write right now - but i want something faster)

    I suppose that's not possible, but maybe someone out ther know of a method.?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    printf() to the rescue.

    Code:
    n = 0
    length = lengthofsubstring
    while (n < strlen)
    {
       printf( "%.*s", length, str + n );
       n += length;
    }
    If the substrings aren't all the same length, then this won't work. If you found out where all the '\n's are and used that information when you print, then it would.
    Last edited by whiteflags; 03-26-2011 at 10:22 PM. Reason: None of your business

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
       printf( "%*.s%n", length, str + n );
    Is it java printf?
    C printf needs 1 arg for %n.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I fixed it. I misplaced the star for some reason.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    In ncurses, you can create rectangular windows which constrain text output in that window to whatever specific area of the screen you specify.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    Stockholm, Sweden
    Posts
    20
    Quote Originally Posted by Salem View Post
    In ncurses, you can create rectangular windows which constrain text output in that window to whatever specific area of the screen you specify.
    Yes, i know about that.

    I prefer to not use that function, because I would not like to initialize structs.

    I initialize the stdscr then I write to it and build my own windows.

    I have not programmed since (before.?) y2k, and I think that i was some kind of confused for what was C programming and C++ programming - so i mixed them a lot to much.

    This time i am writing one program as a starting point, which is a file-viewer.

    First
    I will write it in C without structs and with fixed arrays only.
    After that
    I will rewrite the code in C with structs, and string-pointers.
    After that
    I will rewrite the code in C++
    After that
    I will rewrite the code (if neexded) to compile with QT
    After that
    I will rewrite the code for C#

    After that - new projects in the C-dialect which i felt most comfortable with.

    I have done a function to put as many linefeeds and as many white-spaces as needed into a buffertstring to put formated output where I which.
    The problem is that it breaks whats allready on the left side of where I put the Window. So I have to repaint (a part of) them as well.
    Another solution would just to chop the string's and use the function moveto(y,x) within a loop.

    But I would prefer to do something more intelligent -still without structs in this first try.

    Can i access parts of the stdout as a memmory-buffert.? Or maybe The whole window (stdout, stdscr, ???) and write to a part of it.?
    Last edited by #define whatevr; 03-27-2011 at 05:02 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can, but I wouldn't:

    Because it's non-standard and system dependent
    Because it's ugly code

    Post up an example of your strings/substrings, and also the kind of output you want.

    Let's find a better answer.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by #define whatevr View Post
    First
    I will write it in C without structs and with fixed arrays only.
    After that
    I will rewrite the code in C with structs, and string-pointers.
    After that
    I will rewrite the code in C++
    After that
    I will rewrite the code (if neexded) to compile with QT
    After that
    I will rewrite the code for C#
    Why would you want to? I think you will quickly get bored with all this rewriting.
    I have never touched curses, but stdout is not supposed to display anything. It is just a stream.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Location
    Stockholm, Sweden
    Posts
    20
    Quote Originally Posted by kmdv View Post
    Why would you want to? I think you will quickly get bored with all this rewriting.
    I have never touched curses, but stdout is not supposed to display anything. It is just a stream.
    Why.? It's my method picking up what i could do for ten years ago, and learn it better. And also it make me sort out what comes from where.

    As I for now not use pointers and not use window-definitions from curses, it make me solve logial problems that I would have a harder task to solve when I later write more complex applications.

    To write the viewer is quite fast done, one day of coding or so. When the locical problems is solved i can easily take what I learned when I build a full blown code-editor out of it.

    Also, I am losy at pointers - so if I don't need to learn this now I can get this task by itself when I rewrite the code (eg write the next version).

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by #define whatevr View Post
    As I for now not use pointers and not use window-definitions from curses, it make me solve logial problems that I would have a harder task to solve when I later write more complex applications.
    To want to do anything beyond "hello world" in C without using pointers is a stupid idea. In the process of avoiding pointers, you will just teach yourself inappropriate and silly methods that will not *at all* help you later to "solve logical problems in more complex applications".

    Having a goal is fine, but having a goal that is more important than learning the language sufficiently to accomplish the goal properly is not.

    What I am hearing from you here is this: I want to learn to program the wrong way because the right way seems too complicated, please tell me this is okay.

    Of course, almost everyone is going to tell you the truth: no, it is not okay. Please rethink something.

    Code:
    To write the viewer is  quite fast done, one day of coding or so.
    Ha-ha. I think you are wrong.

    Here's an observation: the ncurses C library is very low level and primitive. It would actually be easier to learn to use a GUI library.

    But my main suggestion is that you consider the aspects of C programming with which you are uncomfortable (pointers, structs, whatever) and go to work on learning their proper use rather than saying "I want to program without pointers and structs for now, until later". If you don' t want to program with pointers and structs right now, don't program at all. Go do something else. If later you feel like trying to understand pointers and structs, then start programming. But if you don't feel like it right now you are just wasting your time doing silly things. Honestly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Mar 2011
    Location
    Stockholm, Sweden
    Posts
    20
    Quote Originally Posted by MK27 View Post
    To want to do anything beyond "hello world" in C without using pointers is a stupid idea. In the process of avoiding pointers, you will just teach yourself inappropriate and silly methods that will not *at all* help you later to "solve logical problems in more complex applications".

    Having a goal is fine, but having a goal that is more important than learning the language sufficiently to accomplish the goal properly is not.

    What I am hearing from you here is this: I want to learn to program the wrong way because the right way seems too complicated, please tell me this is okay.

    Of course, almost everyone is going to tell you the truth: no, it is not okay. Please rethink something.

    Code:
    To write the viewer is  quite fast done, one day of coding or so.
    Ha-ha. I think you are wrong.

    Here's an observation: the ncurses C library is very low level and primitive. It would actually be easier to learn to use a GUI library.

    But my main suggestion is that you consider the aspects of C programming with which you are uncomfortable (pointers, structs, whatever) and go to work on learning their proper use rather than saying "I want to program without pointers and structs for now, until later". If you don' t want to program with pointers and structs right now, don't program at all. Go do something else. If later you feel like trying to understand pointers and structs, then start programming. But if you don't feel like it right now you are just wasting your time doing silly things. Honestly.
    The viewer is Done to 99 %
    I can view all text files no larger than 500 LineBraks and strings as long as 400 chars in several modes.

    Mode 1 (full line) Views the strings without wrap the screen-edges - so I can scroll one column (not one side) back and forth with the keypad. I will add a function to let the user choose how much columns to scroll at one time.

    Mode 2 (Full Line, Fixed Cursor) Let me View the file not wrap around the screen edges wiithout moving the cursor in any direction

    Mode 3 (wrap mode) Let me move up and down with the text wrapped around the edges of the screen.

    Mode 4 (wrap mode, fixed cursor) Let me move up and down with the text wrapped around the edges of the screen With fixed cursor position.

    The problem with mode 3 and 4 is that if I have a Line which breaks below the screen - I don't have the knowhow how to move the last part of teh text One line at a time.
    AS I do now, i store It in a buffer until I go down a Lins, and then I repaint the string which could be - lets say, three full lines of text. The result is not that elegant.

    I know I can use a loop and count down where the max_y is.
    But it seems ineffecient to put it within the Windows-function which I have to write out one page and fill it with text, therefore I wrote this a lite helper function which will do the job.

    As I began write it I wanted to tweak it a little, therefore I come up with my question.

    IF i used the windows defined by structs in curses, then i should not come across this kinda problems and I have not learnt what I am learning now.

    I have used structs and classes a lot before I quit programming, but I feel this is an challange to me.

    I have a clue about pointers, but I feel that there is more to learn, and that 'more to learn' Is what i have planned to spend the next version of this program to.

    And for that thing about rewriting the app on one day, This thingy took me about one week.

    Should I sit down and do it from the beginning It should take me less than one day.
    Heck, it's only about 500 Lines of Code - and the most of it is some loops and switch statements. Not too complicated.

    Thats about your HaHa, and your thinking that I am wrong, which was a little - eh, wrong.

    When I got this version done, which will be in just the time I have solved my problem with properly scroll the Lines which goes outside the screen, I will call it version 0.1, then I will write version 0.2 with pointers, and structs. And this version in it's turn should take about one week - mostly for me getting the time to relearn about pointers.

    And yes, I have used pointers a lot In the Cpp days. In C there needs some more knoweledge than just how to use new and release.

    So - now done with this, I have spent enough time discussing with you and it's time to answer tho one that really wants to help me.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by #define whatevr View Post
    it's time to answer tho one that really wants to help me.
    Trust me, I am. If you run out into traffic blindfolded and someone yells at you to STOP that does not mean they are being unhelpful, trying to make fun of you, etc.

    Mode 1 (full line) Views the strings without wrap the screen-edges - so I can scroll one column (not one side) back and forth with the keypad. I will add a function to let the user choose how much columns to scroll at one time.
    Learning programming involves a lot of repetition, so writing more functions is always good. Just don't get mired down with too much repetition: if writing this program is a learning experience, don't bother with adding features just because you can if you aren't learning something by doing so. This is what I meant by wasting time. Remember, neither you nor anyone else will be making much real use of the product. Your first programs should be focussed on demonstrating specific programming concepts and techniques, not higher level design concerns.

    The problem with mode 3 and 4 is that if I have a Line which breaks below the screen - I don't have the knowhow how to move the last part of teh text One line at a time.
    AS I do now, i store It in a buffer until I go down a Lins, and then I repaint the string which could be - lets say, three full lines of text. The result is not that elegant.
    You need to post some actual code and not describe things to us 2nd hand, otherwise we are just "beating around a bush". Don't be scared, and don't be defensive.

    IF i used the windows defined by structs in curses, then i should not come across this kinda problems and I have not learnt what I am learning now.
    No, in that case you would be learning how to accomplish something properly instead of working around your ignorance and taking a more tedious route, which, as I said before, will have little or no value later on (other than to convince you that learning to do something with one hand tied behind your back is not a good way to learn to use both hands at the same time).

    I have used structs and classes a lot before I quit programming, but I feel this is an challange to me.
    Memorizing Pi to 10000 places would be a challenge too. A good memory exercise, but nothing else. So if your goal is to practice typing and indenting, keep coding with this attitude.

    I have a clue about pointers, but I feel that there is more to learn,
    Yep -- pointers!

    Heck, it's only about 500 Lines of Code - and the most of it is some loops and switch statements. Not too complicated.
    This sounds like the very definition of poorly written, spaghetti like code. Quantity is not quality.

    Thats about your HaHa, and your thinking that I am wrong, which was a little - eh, wrong.
    This depends on what your definition of "finished" is. I could offer to replace the main bearings in your car during lunch break, and if you don't care what happens to it, you might even let me try. I'm not trying to start an argument or mock you, I'm trying to prevent you from getting run over. Really. You are asking for advice, this is it.

    Of course, if you don't like learning, you don't have to. Then the argument is yours .
    Last edited by MK27; 03-27-2011 at 10:43 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Mar 2011
    Location
    Stockholm, Sweden
    Posts
    20

    Thumbs up

    Quote Originally Posted by Adak View Post
    You can, but I wouldn't:

    Because it's non-standard and system dependent
    Because it's ugly code

    Post up an example of your strings/substrings, and also the kind of output you want.

    Let's find a better answer.
    Sorry for Very Very Very late response. I have no excuses - more than that I wasn't
    proud for my code and needed to fix it before posting.
    In the meantime I come across a lot of errors which delayed it all.


    Ok, here comes my code. It's not that good, but good enough to serve as
    a scratch book for what I am going to do in the near future.

    Code:
    //main_W_wrap_primitive
    //read one string in turn from rgwsz[pos_rg][]
    //and pass it to W_wrap_wsz
    
    int main_W_wrap_primitive( int top , int bot , int lef , int rig )
    {
    	int lines=0; 
    	int hight = bot-top;
    	int width = rig- lef;
    	int rgp = pos_rg; 
    	while ( lines < hight )
    	{
    		lines += W_wrap_wsz (top + lines, bot, lef, rig, rgwsz[ rgp ] );
    		if ( (wcslen(rgwsz [ rgp +1]) /( width + lines) ) >= hight)
    			break;
    		rgp++;
    	}
    	return lines; 
    }

    Here is W_wrap_wsz
    Code:
    //W_wrap_wsz, takes a string as input and print it as a window,
    //return number of lines written.
    int W_wrap_wsz (int top, int bot, int lef, int rig, const wchar_t *wsz )
    {
    	wchar_t wsz_tmp[ LINES * COLS ];
    	int beg, end, old_end;
    	int lines=0;
    	beg = 0; 
    	end = rig - lef;
    	old_end = end;
    	if (end > wcslen(wsz)) //wszlen will be replaced with length stored in variable
    	{                                //I have the variable and use it in other pieces
                                              //of my code, just didn't implicate it here yet.
    		print_wsz_YX( top+lines , lef, wsz);
    		lines++;
    		return lines; 
    	}
    	else
    	if ( end < wcslen(wsz) ) 
    	{
    		while (lines < (bot - top) && beg < wcslen(wsz))
    		{
    			wsz_nn_cpy( wsz_tmp, wsz, beg, end);
    			print_wsz_YX( top + lines, lef, wsz_tmp);
    			lines++;
    			beg=end; 
    			end = (end + old_end);
    			if ( lines >= bot - top )
    			{
    				return lines;
    			}
    		}
    	}
    return lines; 
    }
    wsz_nn_cpy calls wscncpy, and adds a '\0'; att the end of the string.
    print_wsz_YX just performes move and prints the string.

    Why I asked about write dierectly to the screen.?
    In the dos-days I created a pointer to the screenbuffert, and if I should write a line at the bottom, I just moved the adress of that pointer (don't remember xactly how I did it) one step. and below that I put the line,s of text.

    I really would love to do the same thing in linux, if that's possible.

    If the code is not portable, well - I could just check for which platform it's compiled under - and If the platform not supported I'll relay on the routines I posted above.

    Is the code ugly.? Well - I think that even if I can't use it in the program- I could learn something from the code.

    I also appreciate all comments about my code.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Location
    Stockholm, Sweden
    Posts
    20
    Quote Originally Posted by MK27 View Post
    Learning programming involves a lot of repetition, so writing more functions is always good. Just don't get mired down with too much repetition: if writing this program is a learning experience, don't bother with adding features just because you can if you aren't learning something by doing so. This is what I meant by wasting time. Remember, neither you nor anyone else will be making much real use of the product. Your first programs should be focussed on demonstrating specific programming concepts and techniques, not higher level design concerns.
    Yes - but I added the function which allow me to scroll at the side - because I didn't know how to solve the problem with scrolling strings which are multiple longer than windows width. And by adding those functions I learned some more about string-management. And also about some of the ncurses routines. This knowledge and the code will be simpler to implant in future. When that day comes, I will ask here how to make it more effective. For now - nah, it's not good enough to ask about.

    Quote Originally Posted by MK27 View Post
    You need to post some actual code and not describe things to us 2nd hand, otherwise we are just "beating around a bush". Don't be scared, and don't be defensive.
    I posted some code in my reply to Adak. But the question about lines below screen is not actual anymore, I solved it in two ways -
    1. I can use my own routines (which is in the reply to Adak), or
    2. I could use the mvwaddnstr routine from ncurses, I am not sure which of them that are fastest, My have a lot of loops and copy actions, but (I suppose) the curses ones just scrolls the string one char at the time - and write that char to the screen).
    And even with the built in routines I need to use loops.

    It should be faster to write a string directly to the window/screen, therefore I did my function which copy strings. I need to do some timings and try to make the routine smaller, which will be done when I re-implement it somewhere else.

    And Yes - i know about the scrollwindow in ncurses, but that to have the same problem.
    Even the VI(m) esditor seems to have some problem with long strings at the bottom.
    My viewer doesn't have that bug.!

    Maybe it's possible to tweak vim to handle the strings correctly.?

    Quote Originally Posted by MK27 View Post
    No, in that case you would be learning how to accomplish something properly instead of working around your ignorance and taking a more tedious route, which, as I said before, will have little or no value later on (other than to convince you that learning to do something with one hand tied behind your back is not a good way to learn to use both hands at the same time).
    Now I learned a lot about the tasks which programming mainly is a part of, the problem solving methods which we use in string routines.
    I have them with me now, and when I write the code in proper way, using the windows defined by ncurses structs instead of just my silly functions - I will re-implant them and ask here how to make them more effective.

    Quote Originally Posted by MK27 View Post
    Memorizing Pi to 10000 places would be a challenge too. A good memory exercise, but nothing else. So if your goal is to practice typing and indenting, keep coding with this attitude.
    No, thats not my goal. But if we go for your example about Pi - Lets say I am taking up math as hobby for the first time about x years, And I was good in Geometry back then, but not that good in - lets say Algebra . Then I would first practice some Geomtry (which in it's turn implicates some very simple algebra) before I go further on. And also - i could use geometry to solve simply problems which better should be solved in other ways - just because I want the fun back in what I do and gather some confidence about the things which I know that I can.

    Quote Originally Posted by MK27 View Post
    Heck, it's only about 500 Lines of Code - and the most of it is some loops and switch statements. Not too complicated.



    This sounds like the very definition of poorly written, spaghetti like code. Quantity is not quality.



    This depends on what your definition of "finished" is. ...
    Who have talked about finished.? I am talking a first - sort off, working version.

    I wrote the first version of my viewer mainly from my own quite bad pseudo-code, that toke me less than 2 hours - and only one compile error before I could try to use it.! But then It shopped of the first top two lines - it couldn't scroll only one line at a time If I had very long strings (those two problems was related to eachother) , and had a lot of other bugsd. But it was a almost working prototype.

    And by th way, It's done now - It's a nice viewer which is quite fast, because It just use fixed-size strings. But I will never say it's "finished", because it was a learning project for me.

    Quote Originally Posted by MK27 View Post
    Of course, if you don't like learning, you don't have to. Then the argument is yours .
    Of course I like learning, Now I am writing the program the RIGHT way - using ncurses, linked lists and pointers - And I am GLAD that I DIDN'T deal with pointers in the beginning - because I had some experiences where my loops searched out of my string's boundary - which gave some sigfults. IF I had begun using pointers allready there, I should have spent lots of unnecessary time looking for why it was not properly initialized - instead of first (re)learn the simple logic about string managements in the first place.

    And - please, don't tell someone that they have to learn YOUR way, some of us just have to get some headache trying to solve things the wrong ways just to find out that what we learned in the past is true.

    The Wrong way which I wrote my fully working viewer in made me solve lot of problems which I will not need spending time to solve when I take up what I forgotten about pointers and linked lists.

    Sorry for the very long time before I replied, But I felt that you was wrong . but had no words to put to it.

    And finally
    STOP DISCOURAGING those that asks for help.
    I Asked explicitly about something else, and I got in an argue with you.
    That was NOT what I wanted.

  15. #15
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    Okay I understand what you're saying, but I have no idea why you'd want to avoid using structs. In a program of this scope, I just don't see a point in not using them.

    In C, structs aren't very complicated at all. You really don't have to worry about them like you do with classes/structs in C++. They are basically just a bunch of variables bundled together.
    Straightforward way to help you avoid having your code littered with repetitive globals.

    Also, what's the benefit to having variables called 'lef' and 'rig' instead of just writing left and right?

    Maybe this is just a style thing, so you can take or leave this advice, but personally I find that my code is significantly more clear if I stick to simple, natural naming conventions.
    But again, that's just a personal thing.

    Lastly, nobody is discouraging you. People are just offering you their advice... so if the shoe fits, wear it, otherwise don't worry about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  3. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  4. Classes and Structs
    By mcorn in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2002, 10:11 AM
  5. Classes or Structs?
    By DeanDemon in forum C++ Programming
    Replies: 12
    Last Post: 12-05-2002, 03:58 AM