printing half a char array

This is a discussion on printing half a char array within the C Programming forums, part of the General Programming Boards category; i only want to print the first 28 characters of my array holding a total of 54 characters using printf. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    10

    printing half a char array

    i only want to print the first 28 characters of my array holding a total of 54 characters using printf.
    is there a printf flag that only prints as many chars from my array that i tell it to?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    something like:

    Code:
    char ch;
    
    ch = arrayname[28];
    arrayname[28] = '\0';
    printf("%s", arrayname);
    arrayname[28] = ch;
    EDIT: Added code tags. . . looked odd without it.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    There is a precision specifier you may use: %.28s -- that sort of thing may be of use.
    [edit]
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char text[] = "All work and no play makes Jack a dull boy.";
       printf("first 28 = \"%.28s\"\n", text);
       return 0;
    }
    
    /* my output
    first 28 = "All work and no play makes J"
    */
    [edit=2 (or so)]
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char text[] = "All work and no play makes Jack a dull boy.";
       printf("first half = \"%.*s\"\n", 28, text);
       return 0;
    }
    
    /* my output
    first half = "All work and no play makes J"
    */
    Might I suggest a standard, n1124.pdf: 7.19.6.1.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    it works so thank you, but in the for loop i have when it stops printing after the 28th letter it doesnt put a new line, but when it doesnt reach 28 it adds a new line i dont know why this is.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I don't suppose you'd care to share this "for loop"?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    i read the strings in off a file and so the new line character is included in the array and gets printed with the other characters, but when i dont print all the characters the new line is not printed either, so how could i test to know wheather to add a new line or not

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    Code:
    void printOneCol(int select[MAXS], int playtime[MAXS], char titles[MAXS][MAXL] )
    {	
    	int i = 0;
    	int mins, secs;
    	while (playtime[i]!=0)/* if playtime ==0 then there is no song there*/
    	{
    		mins = 0;/* tryint to make it obvious if convert method isnt working*/
    		secs = 0;/* by initializeing mins and secs or each new song to 0*/
    		convertSecondsToMinutes(&mins, &secs, playtime[i]);
    		if (select[i] == 1)
    		{
    			printf("*");/* indicates song is selected*/
    		}
    		else
    		{
    			printf(" ");/* holds place of * to keep spaceing same*/
    		}
    		printf("[%1d]%3d:%02d %.28s", i+1, mins, secs,  titles[i] );/* prints song info...*/
    		i++;
    	}
    }
    it outputs
    [1] 3:01 Hey Jude
    [2] 3:00 Stairway to Heaven
    [3] 3:59 Radar Love
    [4] 9:03 Riders On The Storm
    [5] 4:05 What's So Funny? ('bout Pea [6] 8:17 Don't Get Fooled Again
    [7] 21:05 Thick As A Brick
    [8] 18:50 Close To The Edge


    after 5 it messes up

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    its a while loop

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Look for the newline character?

    The FAQ shows one way.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    But if your loop that you refuse to post, you could also look at the current character, and if it is the newline that you don't want to print, then don't print it.


    If you want poor help, don't post code; if you want half-assed help, post parts of code; if you want good help, post actual code; if you want great help, post a minimal snippet of code that actually demonstrates the exact problem and nothing more.
    Last edited by Dave_Sinkula; 11-08-2006 at 09:51 PM. Reason: Damn that last paragraph ended up being pokey.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    sorry
    thanks for the help

    ill look for it

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by theoneandonly
    i only want to print the first 28 characters of my array holding a total of 54 characters using printf.
    is there a printf flag that only prints as many chars from my array that i tell it to?
    Yes, there is (a flag), that's exactly what you're looking for. It's called a field width moderator. Say you want to print a variable (we'll do a number, strings are same), and use a field width moderator so it always will print a 30 char/digit, wide field, no matter the size of the variable itself. (It pads it with blank spaces, on either the left, or the right, depending on the flag for field adjustment - left "-" or right (default):

    Code:
    Read the correcting note at the bottom - sorry to confuse.
    printf("%-30.2f", year06Salary);  /* untested, but should do it */
    The variable will be left justified with the "-", in a field width of 30 chars/digits, and show 2 digits after the decimal point ".2"

    Well that was certainly off-the-mark, eh? The "30" in the above code IS a width modifier, BUT it's only a MINIMUM width, NOT a maximum. The ".2" for strings, is the MAXIMUM number of chars that you want printed. The justification description above, is correct.

    Adak
    Last edited by Adak; 11-09-2006 at 01:59 AM.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Adak
    Yes, there is exactly what you're looking for. It's called a field width moderator. Say you want to print a variable (we'll do a number, strings are same), and use a field width moderator so it always will print a 30 char/digit, wide field, no matter the size of the variable itself. (It pads it with blank spaces, on either the left, or the right, depending on the flag for field adjustment - left "-" or right (default):
    *or*
    Quote Originally Posted by Dave_Sinkula
    Might I suggest a standard, n1124.pdf: 7.19.6.1.
    p4 if you want to be particular.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    i couldnt get that to work, so i went back to %.29s and it works, but i am having trouble testing my array for the \n

    i am trying to test the elements in the array and see if there is a \ followed by an n, and if there isnt that in the first 31 elements i am printing a newline, but it isnt working at all

    titles is a 2d array holding the song names, and at the end is a \n, but when i dont print all the way to the end the \n character isnt printed so there is no newline, and i am just trying to add a new line in this case

    this is what i have...

    Code:
    		for(j=0; j<31; j++)
    		{
    			if(titles[i][j] == '\\' && titles[i][j+1]=='n')
    			{
    				includesNewLine = 1;
    			}	
    		
    		}
    		if(includesNewLine == 0 && j==30)
    			{
    				printf("\n");
    			}
    		i++;
    	}

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A newline is not the two characters \ and n except in string literals in source code. In a file being read it is one or more characters than may be translated -- if the file is opened in text mode -- into a single character represented by the character notation '\n'.

    [edit]Neither of the characters that may combine to represent a newline, generally, is \ nor n.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by theoneandonly
    i couldnt get that to work, so i went back to %.29s and it works, but i am having trouble testing my array for the \n

    i am trying to test the elements in the array and see if there is a \ followed by an n, and if there isnt that in the first 31 elements i am printing a newline, but it isnt working at all

    titles is a 2d array holding the song names, and at the end is a \n, but when i dont print all the way to the end the \n character isnt printed so there is no newline, and i am just trying to add a new line in this case

    this is what i have...

    Code:
    		for(j=0; j<31; j++)
    		{
    			if(titles[i][j] == '\\' && titles[i][j+1]=='n')
    			{
    				includesNewLine = 1;
    			}	
    		
    		}
    		if(includesNewLine == 0 && j==30)
    			{
    				printf("\n");
    			}
    		i++;
    	}
    Yeah, your code is trouble. Newlines are conceptually just one char at the C level. You can find it by using a small addition to the classic:

    Code:
    while(((ch = getchar()) != '\n') && (j < 30)); /* untested, but should be close */
    However, it seems there is an easier way if this attacks your brain. If these song titles are proper strings (that is, they have an end of string marker '\0' as their last char, then it's just a matter of:

    1) print the first 29 char's of the song title (which you're up to snuff on), and if the song has fewer, the print function will add in blank spaces to fill the field modifier. Then:

    2) for every song you print out, just print a newline '\n', and you're cooking with gas, now.

    They'll all need it, because no song title string will have a newline in it. If they don't have proper EOString char's attached to them, why not code up a function to do just that?

    YMMV, but when I'm working with strings, I definitely want them to be elevated to proper string status by having an EndOfString marker, tagged on to them.

    If you program much, you may see DOS style text END of LINE markers as two int's: 10 and 13, which represented Carriage Return, and Line Feed, iirc. This can be confusing if you've not seen it before. In general, I've found it helpful to take lines of char's as strings, which makes the (10 & 13 CR/LF) combo, be seen in C as just the old familiar '\n' in your last char of the line.

    Adak

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 02:23 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21