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?
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?
something like:
EDIT: Added code tags. . . looked odd without it.Code:char ch; ch = arrayname[28]; arrayname[28] = '\0'; printf("%s", arrayname); arrayname[28] = ch;
There is a precision specifier you may use: %.28s -- that sort of thing may be of use.
[edit][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 28 = \"%.28s\"\n", text); return 0; } /* my output first 28 = "All work and no play makes J" */Might I suggest a standard, n1124.pdf: 7.19.6.1.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" */
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.*
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.
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.*
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
it outputsCode: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++; } }
[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
its a while loop
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 10: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.*
sorry
thanks for the help
ill look for it
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):Originally Posted by theoneandonly
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"Code:Read the correcting note at the bottom - sorry to confuse. printf("%-30.2f", year06Salary); /* untested, but should do it */
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 02:59 AM.
*or*Originally Posted by Adakp4 if you want to be particular.Originally Posted by Dave_Sinkula
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.*
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++; }
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.*
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:Originally Posted by theoneandonly
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:Code:while(((ch = getchar()) != '\n') && (j < 30)); /* untested, but should be close */
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