Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Terminating a String

Status
Not open for further replies.

JimJoyce

Instructor
Feb 17, 2006
4
GB
I feel such a fool.
Up till a few years ago I used to be a C Instructor.
Now my C is very rusty.

If I strcpy part of a string into an empty buffer,
how do I place a '\0' at the end of the copied data?
 
Don't worry: strcpy adds it. It's a part of the strcpy RTL function specification.
 
I'm not so sure.
My output appears to be one long string.

I have a source-file (Dates of Easter, from a web site)

Code:
23 Apr 2000   20 Apr 2025   10 Apr 2050    7 Apr 2075
15 Apr 2001    5 Apr 2026    2 Apr 2051   19 Apr 2076
31 Mar 2002   28 Mar 2027   21 Apr 2052   11 Apr 2077
20 Apr 2003   16 Apr 2028    6 Apr 2053    3 Apr 2078
11 Apr 2004    1 Apr 2029   29 Mar 2054   23 Apr 2079
27 Mar 2005   21 Apr 2030   18 Apr 2055    7 Apr 2080
16 Apr 2006   13 Apr 2031    2 Apr 2056   30 Mar 2081
 8 Apr 2007   28 Mar 2032   22 Apr 2057   19 Apr 2082
23 Mar 2008   17 Apr 2033   14 Apr 2058    4 Apr 2083
12 Apr 2009    9 Apr 2034   30 Mar 2059   26 Mar 2084
 4 Apr 2010   25 Mar 2035   18 Apr 2060   15 Apr 2085
24 Apr 2011   13 Apr 2036   10 Apr 2061   31 Mar 2086
 8 Apr 2012    5 Apr 2037   26 Mar 2062   20 Apr 2087
31 Mar 2013   25 Apr 2038   15 Apr 2063   11 Apr 2088
20 Apr 2014   10 Apr 2039    6 Apr 2064    3 Apr 2089
 5 Apr 2015    1 Apr 2040   29 Mar 2065   16 Apr 2090
27 Mar 2016   21 Apr 2041   11 Apr 2066    8 Apr 2091
16 Apr 2017    6 Apr 2042    3 Apr 2067   30 Mar 2092
 1 Apr 2018   29 Mar 2043   22 Apr 2068   12 Apr 2093
21 Apr 2019   17 Apr 2044   14 Apr 2069    4 Apr 2094
12 Apr 2020    9 Apr 2045   30 Mar 2070   24 Apr 2095
 4 Apr 2021   25 Mar 2046   19 Apr 2071   15 Apr 2096
17 Apr 2022   14 Apr 2047   10 Apr 2072   31 Mar 2097
 9 Apr 2023    5 Apr 2048   26 Mar 2073   20 Apr 2098
31 Mar 2024   18 Apr 2049   15 Apr 2074   12 Apr 2099

I want to convert it into a 100 line file.

23 Apr 2000
15 Apr 2001
...
31 Mar 2024
20 Apr 2025
...
12 Apr 2099

I need to substring the 25 lines
and rearrange them as 100 lines
This is my code:

Code:
#include <stdio.h>

int main( )
   {
   FILE *fin, *fout;
   int  j, k, n;
   char buff[60];
   char yrs[25][4][12];
   char cent[100][12];
  
   fin  = fopen ( "east.txt", "r" );
   fout = fopen ( "e600.txt", "w" );

   for ( k=0 ; k<25 ; k++ )
      {
      fgets ( buff, 59, fin );
//puts ( buff );
      for ( n=0 ; n<4 ; n++ )
         {
         strncpy ( yrs[k][n], buff+n*12, 12 );
//puts ( yrs[k][n] );
         }
      }

   for ( n=0 ; n<4 ; n++ )
      {
      for ( k=0 ; k<25 ; k++ )
         {
      strcpy ( cent[n*25+k], yrs[k][n] );
//puts ( cent[n*25+k] );
         }
      }

   fputs ( buff, fout );
   for ( k=0 ; k<100 ; k++ )
      {
      fputs ( cent[k], fout );
//puts ( cent[k] );
      }

   fclose ( fin );
   fclose ( fout );
   }

The four //puts(), in column one, are purely trace code.

 
> char yrs[25][4][12];
> char cent[100][12];
Well both need to be 13 characters long to store the \0

> strncpy ( yrs[k][n], buff+n*12, 12 );
This doesn't copy a \0 by default. It's easy to do with
[tt]yrs[k][n][12] = '\0';[/tt]

>
Code:
   for ( n=0 ; n<4 ; n++ )
      {
      for ( k=0 ; k<25 ; k++ )
         {
      strcpy ( cent[n*25+k], yrs[k][n] );
If you swap the n and k loops around, you can just print directly from the yrs array.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Thanks, Salem.
That yrs[k][n][12] is the code I wanted.
I'd forgotten how to reference that nested array.

I'm not sure I need the arrays to be [13]
The strings are only 11 chars long.

I'll give them both a try.
 
JimJoyce said:
The strings are only 11 chars long.
Then why are you telling strncpy() to copy 12 characters instead of 11?
 
Thanks cpjust,
As I said right at the beginning of the thread...
I feel such a fool !!!

Program's now working. It reads 600 years of easters,
which I'm analysing for frequencies and repetitions.

Many thanks to All you supporters,
JimJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top