By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,105 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,105 IT Pros & Developers. It's quick & easy.

ignore a line

P: n/a
#include <stdio.h>
int file_copy( char *oldname, char *newname );

int main()
{
char source[80], destination[80];

printf("\nEnter source file: ");
gets(source);
printf("\nEnter destination file: ");
gets(destination);

if ( file_copy(source, destination )==0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
system("PAUSE");
return(0);
}

int file_copy( char *oldname, char *newname)
{
FILE *fold, *fnew;
int c;
char tempString[150];

if ( ( fold = fopen( oldname, "r")) == NULL)
{
printf("Source file is not open\n");
return -1;
}
if ((fnew = fopen(newname, "w")) == NULL)
{
printf("Destination file is not open\n");
fclose(fold);
return -1;
}

while(1)
{
/* I want to ignore a line */
fgets(tempString, 130, fold);

c=fgetc(fold);

if(!feof(fold))
fputc(c, fnew);
else
break;
}
fclose(fnew);
fclose(fold);

return 0;
}

In the above program, I want to open a file and copy to a new file.
The new file is the same as the old file except the first line is ignored
from the old file.
How can I do that?

The above program works well of just copying old file to new file if I
delete this line(which I want to ignore the first line but failed):

/* I want to ignore a line */
fgets(tempString, 130, fold);

how can I do that?
thx!!
Nov 13 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:
#include <stdio.h>
int file_copy( char *oldname, char *newname );

int main()
{
char source[80], destination[80];
Magic number 80. A filename may well exceed 79+1 characters
printf("\nEnter source file: ");
gets(source);
Warning: possible buffer overrun.
Never ever use gets, it'll get you into severe trouble if a user enters
a string larger than the buffer you provide.
Please read the c.l.c-faq http://www.eskimo.com/~scs/C-faq/top.html ,
section 12.23 about this issue.
printf("\nEnter destination file: ");
gets(destination);
See above.

if ( file_copy(source, destination )==0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
system("PAUSE");
return(0);
}

int file_copy( char *oldname, char *newname)
{
FILE *fold, *fnew;
int c;
char tempString[150];
Magic number 150. The line of input you attempt to read into tempString
may well exceed a length of 149+1.

if ( ( fold = fopen( oldname, "r")) == NULL)
{
printf("Source file is not open\n");
return -1;
}
if ((fnew = fopen(newname, "w")) == NULL)
{
printf("Destination file is not open\n");
fclose(fold);
return -1;
}

while(1)
It's good coding pratice to provide a loop statement with a proper
condition that makes it possible to bail out (except for loops that
have to run "forever", but that's a different matter).
{
/* I want to ignore a line */
fgets(tempString, 130, fold);
Why magic number 130 here, instead of 150 as above?
What if the line you are reading exceeds the limit of 130 chars?
Why use fgets() (with a possibly inappropriate sized buffer) when you
perform the copying itself character-wise?

Now for the RealProblem[tm]:
You are skipping one line of input before /every/ single character
you copy, this is definitely not what you want.

c=fgetc(fold);

if(!feof(fold))
fputc(c, fnew);
else
break;
}
What about replacing the above loop with:

/* ignore first line: */
while ( ( c = fgetc( fold ) ) != EOF )
if ( c == '\n' )
break;

/* copy remaining content: */
while ( ( c = fgetc( fold ) ) != EOF )
fputc( c, fnew );

/* I strongly suggest checking the error indicators for both
streams with the ferror function here. */
fclose(fnew);
fclose(fold);

return 0;
}

In the above program, I want to open a file and copy to a new file.
The new file is the same as the old file except the first line is ignored
from the old file.
How can I do that?
See above.
The above program works well of just copying old file to new file if I
delete this line(which I want to ignore the first line but failed):

/* I want to ignore a line */
fgets(tempString, 130, fold);

how can I do that?
See above.
thx!!


You're welcome.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #2

P: n/a

"Irrwahn Grausewitz" <ir*******@freenet.de> ???
news:16********************************@4ax.com ???...
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:
#include <stdio.h>
int file_copy( char *oldname, char *newname );

int main()
{
char source[80], destination[80];
Magic number 80. A filename may well exceed 79+1 characters

printf("\nEnter source file: ");
gets(source);


Warning: possible buffer overrun.
Never ever use gets, it'll get you into severe trouble if a user enters
a string larger than the buffer you provide.
Please read the c.l.c-faq http://www.eskimo.com/~scs/C-faq/top.html ,
section 12.23 about this issue.
printf("\nEnter destination file: ");
gets(destination);


See above.

if ( file_copy(source, destination )==0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
system("PAUSE");
return(0);
}

int file_copy( char *oldname, char *newname)
{
FILE *fold, *fnew;
int c;
char tempString[150];


Magic number 150. The line of input you attempt to read into tempString
may well exceed a length of 149+1.

if ( ( fold = fopen( oldname, "r")) == NULL)
{
printf("Source file is not open\n");
return -1;
}
if ((fnew = fopen(newname, "w")) == NULL)
{
printf("Destination file is not open\n");
fclose(fold);
return -1;
}

while(1)


It's good coding pratice to provide a loop statement with a proper
condition that makes it possible to bail out (except for loops that
have to run "forever", but that's a different matter).
{
/* I want to ignore a line */
fgets(tempString, 130, fold);


Why magic number 130 here, instead of 150 as above?
What if the line you are reading exceeds the limit of 130 chars?
Why use fgets() (with a possibly inappropriate sized buffer) when you
perform the copying itself character-wise?

Now for the RealProblem[tm]:
You are skipping one line of input before /every/ single character
you copy, this is definitely not what you want.

c=fgetc(fold);

if(!feof(fold))
fputc(c, fnew);
else
break;
}


What about replacing the above loop with:

/* ignore first line: */
while ( ( c = fgetc( fold ) ) != EOF )
if ( c == '\n' )
break;

/* copy remaining content: */
while ( ( c = fgetc( fold ) ) != EOF )
fputc( c, fnew );

/* I strongly suggest checking the error indicators for both
streams with the ferror function here. */


If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I insert
the code?
the atoi() function seems only works with string to integer but not single
character to integer

thx!
fclose(fnew);
fclose(fold);

return 0;
}

In the above program, I want to open a file and copy to a new file.
The new file is the same as the old file except the first line is ignored
from the old file.
How can I do that?


See above.
The above program works well of just copying old file to new file if I
delete this line(which I want to ignore the first line but failed):

/* I want to ignore a line */
fgets(tempString, 130, fold);

how can I do that?


See above.
thx!!


You're welcome.

Regards
--
Irrwahn
(ir*******@freenet.de)

Nov 13 '05 #3

P: n/a
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> ???
news:16********************************@4ax.com ???... <SNIP>

/* Put this line at the top of your program: */

#include <ctype.h>
/* Put these lines at the top of your function: */

int line = 1;
int pos = 1;
int val70 = 0;

<SNIP>

/* ignore first line: */
while ( ( c = fgetc( fold ) ) != EOF )
if ( c == '\n' ) {
++line; break; }

/* copy remaining content: */
while ( ( c = fgetc( fold ) ) != EOF ) {
/* save digit at L2:C70 in val70: */
if ( line == 2 && pos == 70 && isdigit(c) )
val70 = c - '0';
if ( c == '\n' )
{
++line_count;
pos = 1;
}
++pos; fputc( c, fnew ); }
/* I strongly suggest checking the error indicators for both
streams with the ferror function here. */


If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I insert
the code?


See above, and please note the following:

1. The code I added assumes that you still want to copy the contents of
the second line to your new file.

2. The code doesn't check if the seventieth character on the second
line even exists.

3. I didn't test the code, it may not work.

4. I used bad coding style in that I use magic numbers and keep carrying
around and incrementing counters that are no longer needed.

So, feel free to improve it. :)
the atoi() function seems only works with string to integer but not single
character to integer


Well, you are free to "wrap" a single character in a string and pass
this to atoi(), but I consider this to be overkill in this case.

<SNIP>

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #4

P: n/a
On Fri, 10 Oct 2003 19:52:45 +0800, "FrancisC"
<fr**********@hong-kong.crosswinds.net> wrote:
I want to open a file and copy to a new file.
The new file is the same as the old file except the first line is ignored
from the old file. #include <stdio.h>
You also need to #include stdlib.h for system().

Preferably, just delete the system() call in your main()--it isn't really
needed, and while system() is standard C, PAUSE is not.

int file_copy( char *oldname, char *newname );

int main()
int main(void)
{
char source[80], destination[80];
Instead of using so-called "magic numbers," #define these constants; i.e.,

#define FILENAME_LEN 256
#define LINELEN 1025

printf("\nEnter source file: "); fflush(stdout); gets(source);
Without a terminating newline in the printf string, you are not guaranteed
to see your prompt before the program waits for your input. Either add a
newline at the end of your prompt, or better yet, explicitly flush the
output stream before calling for input, as I've done above.

Also, instead of using Microsoft's favorite function gets(), use fgets().
gets does not protect you from buffer overruns; fgets does.

#include <string.h>
...

if (fgets(source, FILENAME_LEN, stdin) != NULL)
source[strlen(source) - 1] = '\0'; // truncate terminal newline
if ( file_copy(source, destination )==0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
Again, flush your output--add a newline to the end of your error string.
system("PAUSE");
return(0);
}
This is a bit of a quibble, but return is a keyword, not a function.
Unless you are trying to modify precedence in a complex expression, the
parentheses are not needed:

return 0;
int file_copy( char *oldname, char *newname)
{
FILE *fold, *fnew;
int c;
char tempString[150];
See above regarding "magic numbers."
if ( ( fold = fopen( oldname, "r")) == NULL)
{
printf("Source file is not open\n");
return -1;
}
if ((fnew = fopen(newname, "w")) == NULL)
{
printf("Destination file is not open\n");
fclose(fold);
Nice catch, closing the already open fold stream before exiting your
function prematurely. Many people would forget to do this.

Now, for your main problem:
while(1)
{
/* I want to ignore a line */
fgets(tempString, 130, fold);

c=fgetc(fold);

if(!feof(fold))
fputc(c, fnew);
else
break;
}


It would be much easier to copy your file on a line rather than character
basis. You would set a flag before beginning the copy, and have your code
check this flag before writing a line to the destination file. If it is
set, clear the flag and skip writing that line to the destination file.
Otherwise, write that line to the destination file.

/* Copy lines from old to new file.
Ignore first line from old file (firstline != 0).
*/

int retval = 0;
int firstline = 1;
char tempString[LINELEN];

/* .. */

while (fgets(tempString, LINELEN, fold))
{
if (firstline) /* Skip first line */
{
firstline = 0;
continue;
}

if (fputs(tempString, fnew) == EOF)
{
fprintf(stderr, "Error writing to %s\n", newname);
retval = -1;
break;
}
}

/* ... */

return retval;
--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 13 '05 #5

P: n/a

"Irrwahn Grausewitz" <ir*******@freenet.de> ???
news:re********************************@4ax.com ???...
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> ???
news:16********************************@4ax.com ???... <SNIP>

/* Put this line at the top of your program: */

#include <ctype.h>
/* Put these lines at the top of your function: */

int line = 1;
int pos = 1;
int val70 = 0;

<SNIP>

/* ignore first line: */
while ( ( c = fgetc( fold ) ) != EOF )
if ( c == '\n' ) {
++line; break; }

/* copy remaining content: */
while ( ( c = fgetc( fold ) ) != EOF ) {
/* save digit at L2:C70 in val70: */
if ( line == 2 && pos == 70 && isdigit(c) )
val70 = c - '0';
if ( c == '\n' )
{
++line_count;
pos = 1;
}
++pos; fputc( c, fnew ); }
/* I strongly suggest checking the error indicators for both
streams with the ferror function here. */


If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I insertthe code?


See above, and please note the following:

1. The code I added assumes that you still want to copy the contents of
the second line to your new file.

2. The code doesn't check if the seventieth character on the second
line even exists.

3. I didn't test the code, it may not work.

4. I used bad coding style in that I use magic numbers and keep carrying
around and incrementing counters that are no longer needed.

So, feel free to improve it. :)
the atoi() function seems only works with string to integer but not singlecharacter to integer


Well, you are free to "wrap" a single character in a string and pass
this to atoi(), but I consider this to be overkill in this case.

while ( ( c = fgetc( fold ) ) != EOF )
{
/* save digit at L2:C70 in val70: */
if ( pos == 70 && isdigit(c) )
{
val70 = c - '0';
}
++pos;
}

I got the 70th character(which is "7" in the first line of the below file's
portion) using the above program part you suggested
but I get lost because I don't know how to obtain the 2 float numbers in the
next line using
fscanf(fold, " %lf %lf", &a, &b);

1 1 0 0 0 0 7
8.3261467E+05 8.2320352E+05 8.3264626E+05 8.2311602E+05
8.3257224E+05 8.2309011E+05 8.3256038E+05 8.2312889E+05

I have tried
while ( ( c = fgetc( fold ) ) != EOF )
{
if ( c == '\n' )
break;
}
after the program part but it does not work
what should I do?
thx!!


<SNIP>

Regards
--
Irrwahn
(ir*******@freenet.de)

Nov 13 '05 #6

P: n/a
Robert B. Clark <ep****@3pynexf.pbz> wrote:
<SNIP>

It would be much easier to copy your file on a line rather than character
basis. You would set a flag before beginning the copy, and have your code
check this flag before writing a line to the destination file. If it is
set, clear the flag and skip writing that line to the destination file.
Otherwise, write that line to the destination file.

/* Copy lines from old to new file.
Ignore first line from old file (firstline != 0).
*/

int retval = 0;
int firstline = 1;
char tempString[LINELEN];

/* .. */

while (fgets(tempString, LINELEN, fold))
{
if (firstline) /* Skip first line */
{
firstline = 0;
continue;
}
Nice try, but how do know you have read the /entire/ first line?
It may well consist of more than LINELEN-1 characters. What about:

if ( firstline ) /* Skip first line */
{
if ( strchr( tempString, '\n' ) )
firstline = 0;
continue;
}

if (fputs(tempString, fnew) == EOF)
{
fprintf(stderr, "Error writing to %s\n", newname);
retval = -1;
break;
}
}

/* ... */

return retval;


Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #7

P: n/a
FrancisC wrote:

[gigantic snip]
If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I insert
the code?


[another big snip]
Please trim down the quoted material to a minimal subset that is
necessary for your reply.


Brian Rodenborn
Nov 13 '05 #8

P: n/a
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:

"Irrwahn Grausewitz" <ir*******@freenet.de> ???
news:re********************************@4ax.com ???...
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:
>"Irrwahn Grausewitz" <ir*******@freenet.de> ???
>news:16********************************@4ax.com ???... <SNIP>

/* Put this line at the top of your program: */

#include <ctype.h>
/* Put these lines at the top of your function: */

int line = 1;
int pos = 1;
int val70 = 0;

<SNIP>
>>
>> /* ignore first line: */
>> while ( ( c = fgetc( fold ) ) != EOF )
>> if ( c == '\n' )

{
++line;
>> break;

}
>>
>> /* copy remaining content: */
>> while ( ( c = fgetc( fold ) ) != EOF )

{
/* save digit at L2:C70 in val70: */
if ( line == 2 && pos == 70 && isdigit(c) )
val70 = c - '0';
if ( c == '\n' )
{
++line_count;
pos = 1;
}
++pos;
>> fputc( c, fnew );

}
>>
>> /* I strongly suggest checking the error indicators for both
>> streams with the ferror function here. */
>
>If I want to copy the 70th character of the second line, and store it as
>integer(eg, "6" to 6) for later writing to a new file, then how can Iinsert >the code?


See above, and please note the following:

1. The code I added assumes that you still want to copy the contents of
the second line to your new file.

2. The code doesn't check if the seventieth character on the second
line even exists.

3. I didn't test the code, it may not work.

4. I used bad coding style in that I use magic numbers and keep carrying
around and incrementing counters that are no longer needed.

So, feel free to improve it. :)
>the atoi() function seems only works with string to integer but notsingle >character to integer


Well, you are free to "wrap" a single character in a string and pass
this to atoi(), but I consider this to be overkill in this case.

while ( ( c = fgetc( fold ) ) != EOF )
{
/* save digit at L2:C70 in val70: */
if ( pos == 70 && isdigit(c) )
{
val70 = c - '0';
}
++pos;
}

I got the 70th character(which is "7" in the first line of the below file's
portion) using the above program part you suggested
but I get lost because


you probably didn't realise that the above loop runs until either EOF is
encountered or an read error occurs.
I don't know how to obtain the 2 float numbers in the
next line using
fscanf(fold, " %lf %lf", &a, &b);
Of course you know how to do that, but the problem is that after the
code above has executed there's nothing left to read.

1 1 0 0 0 0 7
8.3261467E+05 8.2320352E+05 8.3264626E+05 8.2311602E+05
8.3257224E+05 8.2309011E+05 8.3256038E+05 8.2312889E+05

I have tried
while ( ( c = fgetc( fold ) ) != EOF )
{
if ( c == '\n' )
break;
}
This code skips to the end of line or EOF. But, again, that only
makes sense if you didn't already hit EOF in preceding code.
after the program part but it does not work
what should I do?
thx!!


Well, the whole thing is getting messy now; when you started the
thread you just wanted to copy one file to another, ignoring the first
line. Now you want to read and convert numerals from it. While this
is of course possible using intermixed calls to fscanf() and fget*(),
this practice easily leads to confusion. Maybe you should stick to
Robert B. Clark's suggestion of reading the file line-wise (but note
my additional reply) and then parse the lines, e.g. using sscanf() or,
preferably, the strtod/strtol function family.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #9

P: n/a
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote in message
news:bm**********@news.hgc.com.hk...

... I want to open a file and copy to a new file.
The new file is the same as the old file except the first line is ignored
from the old file.
How can I do that?


fscanf(fp, "%*[^\n]");

--
Peter
Nov 13 '05 #10

P: n/a
<SNIP>

/* Put this line at the top of your program: */

#include <ctype.h>
/* Put these lines at the top of your function: */

int line = 1;
int pos = 1;
int val70 = 0;

<SNIP>

>>
>> /* ignore first line: */
>> while ( ( c = fgetc( fold ) ) != EOF )
>> if ( c == '\n' )
{
++line;
>> break;
}

>>
>> /* copy remaining content: */
>> while ( ( c = fgetc( fold ) ) != EOF )
{
/* save digit at L2:C70 in val70: */
if ( line == 2 && pos == 70 && isdigit(c) )
val70 = c - '0';
if ( c == '\n' )
{
++line_count;
pos = 1;
}
++pos;
>> fputc( c, fnew );
}
>>
>> /* I strongly suggest checking the error indicators for both
>> streams with the ferror function here. */
>
>If I want to copy the 70th character of the second line, and store it as >integer(eg, "6" to 6) for later writing to a new file, then how can I

insert
>the code?

See above, and please note the following:

1. The code I added assumes that you still want to copy the contents of
the second line to your new file.

2. The code doesn't check if the seventieth character on the second
line even exists.

3. I didn't test the code, it may not work.

4. I used bad coding style in that I use magic numbers and keep carrying around and incrementing counters that are no longer needed.

So, feel free to improve it. :)

>the atoi() function seems only works with string to integer but not

single
>character to integer

Well, you are free to "wrap" a single character in a string and pass
this to atoi(), but I consider this to be overkill in this case.

while ( ( c = fgetc( fold ) ) != EOF )
{
/* save digit at L2:C70 in val70: */
if ( pos == 70 && isdigit(c) )
{
val70 = c - '0';
}
++pos;
}
while ( ( c = fgetc( fold ) ) != '\n' )
{
/* save digit at L2:C70 in val70: */
if ( pos == 70 && isdigit(c) )
{
val70 = c - '0';
}
++pos;
}

To make it more complicated and useful, I want to get the 68th and 69th also
both 68th and 69th are either a " " or a digital character like "5"
so from the 68th to 70th characters, it is in the format like "123", " 23"
or " 3"
and I want to store the string in integer like 123, 23 or 3

so I do some stupid coding like this:

while ( ( c = fgetc( fold ) ) != '\n' )
{
/* save C68 in char1 */
if ( pos == 68 && isdigit(c) )
{
char1 = c ;
}

/* save C69 in char2 */
if ( pos == 69 && isdigit(c) )
{
char2 = c ;
printf("%d\n", noOfCoordinate);
}

/* save C70 in char3: */
if ( pos == 70 && isdigit(c) )
{
char3 = c;
integerVal= atoi((char1+char2+char3));
}
++pos;
}

but it does not work out
what should be the coding look like?
I have no idea, thx!

I got the 70th character(which is "7" in the first line of the below file'sportion) using the above program part you suggested
but I get lost because


you probably didn't realise that the above loop runs until either EOF is
encountered or an read error occurs.
I don't know how to obtain the 2 float numbers in the
next line using
fscanf(fold, " %lf %lf", &a, &b);


Of course you know how to do that, but the problem is that after the
code above has executed there's nothing left to read.

1 1 0 0 0 0 7
8.3261467E+05 8.2320352E+05 8.3264626E+05 8.2311602E+05
8.3257224E+05 8.2309011E+05 8.3256038E+05 8.2312889E+05

I have tried
while ( ( c = fgetc( fold ) ) != EOF )
{
if ( c == '\n' )
break;
}


This code skips to the end of line or EOF. But, again, that only
makes sense if you didn't already hit EOF in preceding code.
after the program part but it does not work
what should I do?
thx!!


Well, the whole thing is getting messy now; when you started the
thread you just wanted to copy one file to another, ignoring the first
line. Now you want to read and convert numerals from it. While this
is of course possible using intermixed calls to fscanf() and fget*(),
this practice easily leads to confusion. Maybe you should stick to
Robert B. Clark's suggestion of reading the file line-wise (but note
my additional reply) and then parse the lines, e.g. using sscanf() or,
preferably, the strtod/strtol function family.

Regards
--
Irrwahn
(ir*******@freenet.de)

Nov 13 '05 #11

P: n/a
> <SNIP>
>
> /* Put this line at the top of your program: */
>
> #include <ctype.h>
>
>
> /* Put these lines at the top of your function: */
>
> int line = 1;
> int pos = 1;
> int val70 = 0;
>
> <SNIP>
>
> >>
> >> /* ignore first line: */
> >> while ( ( c = fgetc( fold ) ) != EOF )
> >> if ( c == '\n' )
> {
> ++line;
> >> break;
> }
>
> >>
> >> /* copy remaining content: */
> >> while ( ( c = fgetc( fold ) ) != EOF )
> {
> /* save digit at L2:C70 in val70: */
> if ( line == 2 && pos == 70 && isdigit(c) )
> val70 = c - '0';
> if ( c == '\n' )
> {
> ++line_count;
> pos = 1;
> }
> ++pos;
> >> fputc( c, fnew );
> }
> >>
> >> /* I strongly suggest checking the error indicators for both> >> streams with the ferror function here. */
> >
> >If I want to copy the 70th character of the second line, and store it
as
> >integer(eg, "6" to 6) for later writing to a new file, then how can
Iinsert
> >the code?
>
> See above, and please note the following:
>
> 1. The code I added assumes that you still want to copy the contents of> the second line to your new file.
>
> 2. The code doesn't check if the seventieth character on the second
> line even exists.
>
> 3. I didn't test the code, it may not work.
>
> 4. I used bad coding style in that I use magic numbers and keep
carrying> around and incrementing counters that are no longer needed.
>
> So, feel free to improve it. :)
>
> >the atoi() function seems only works with string to integer but not
single
> >character to integer
>
> Well, you are free to "wrap" a single character in a string and pass
> this to atoi(), but I consider this to be overkill in this case.
while ( ( c = fgetc( fold ) ) != EOF )
{
/* save digit at L2:C70 in val70: */
if ( pos == 70 && isdigit(c) )
{
val70 = c - '0';
}
++pos;
}
while ( ( c = fgetc( fold ) ) != '\n' )
{
/* save digit at L2:C70 in val70: */
if ( pos == 70 && isdigit(c) )
{
val70 = c - '0';
}
++pos;
}

To make it more complicated and useful, I want to get the 68th and 69th

also both 68th and 69th are either a " " or a digital character like "5"
so from the 68th to 70th characters, it is in the format like "123", " 23"
or " 3"
and I want to store the string in integer like 123, 23 or 3

so I do some stupid coding like this:

while ( ( c = fgetc( fold ) ) != '\n' )
{
/* save C68 in char1 */
if ( pos == 68 && isdigit(c) )
{
char1 = c ;
}

/* save C69 in char2 */
if ( pos == 69 && isdigit(c) )
{
char2 = c ;
printf("%d\n", noOfCoordinate);
}

/* save C70 in char3: */
if ( pos == 70 && isdigit(c) )
{
char3 = c;
integerVal= atoi((char1+char2+char3));
}
++pos;
}

but it does not work out
what should be the coding look like?
I have no idea, thx!


I have just think of an idea, it seems work, but I don't know whether this
method is stupid or not:

while ( ( c = fgetc( fold ) ) != '\n' )
{
/* save digit at C68 in int1 */
if ( pos == 68 && isdigit(c) )
{
int1 = c - '0';
}

/* save digit at C69 in int2 */
if ( pos == 69 && isdigit(c) )
{
int2 = c - '0';
}

/* save digit at C70 in int3: */
if ( pos == 70 && isdigit(c) )
{
int3 = c - '0';
integerVal= (int1*100) + (int2*10) + int3 );
}
++pos;
}

/* set the values for the repeat loop for later use */
pos = 1;
int1 = 0;
int2 = 0;


I got the 70th character(which is "7" in the first line of the below file'sportion) using the above program part you suggested
but I get lost because


you probably didn't realise that the above loop runs until either EOF is
encountered or an read error occurs.
I don't know how to obtain the 2 float numbers in the
next line using
fscanf(fold, " %lf %lf", &a, &b);


Of course you know how to do that, but the problem is that after the
code above has executed there's nothing left to read.

1 1 0 0 0 0 7
8.3261467E+05 8.2320352E+05 8.3264626E+05 8.2311602E+05
8.3257224E+05 8.2309011E+05 8.3256038E+05 8.2312889E+05

I have tried
while ( ( c = fgetc( fold ) ) != EOF )
{
if ( c == '\n' )
break;
}


This code skips to the end of line or EOF. But, again, that only
makes sense if you didn't already hit EOF in preceding code.
after the program part but it does not work
what should I do?
thx!!


Well, the whole thing is getting messy now; when you started the
thread you just wanted to copy one file to another, ignoring the first
line. Now you want to read and convert numerals from it. While this
is of course possible using intermixed calls to fscanf() and fget*(),
this practice easily leads to confusion. Maybe you should stick to
Robert B. Clark's suggestion of reading the file line-wise (but note
my additional reply) and then parse the lines, e.g. using sscanf() or,
preferably, the strtod/strtol function family.

Nov 13 '05 #12

P: n/a
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote: <SNIIIIP>
To make it more complicated and useful, I want to get the 68th and 69th
also both 68th and 69th are either a " " or a digital character like "5"
so from the 68th to 70th characters, it is in the format like "123", " 23"
or " 3"
and I want to store the string in integer like 123, 23 or 3

so I do some stupid coding like this:

while ( ( c = fgetc( fold ) ) != '\n' )
{
/* save C68 in char1 */
if ( pos == 68 && isdigit(c) )
{
char1 = c ;
}

/* save C69 in char2 */
if ( pos == 69 && isdigit(c) )
{
char2 = c ;
printf("%d\n", noOfCoordinate);
}

/* save C70 in char3: */
if ( pos == 70 && isdigit(c) )
{
char3 = c;
integerVal= atoi((char1+char2+char3));
}
++pos;
}

but it does not work out
what should be the coding look like?
I have no idea, thx!


Uh-oh, you cannot use the plus operator to concatenate characters to
form a string; /if/ you want to do it like this, then just copy your
digits into a string. Just for fun (we will forget about it later),
here's some code to illustrate what I mean:

char number[ 4 ];
int cnt = 0,
intVal;

/* ... */

while ( ( c = fgetc( fold ) ) != '\n' )
{
/* save C68-70 in number[]: */
if ( pos > 67 && pos < 71 )
number[ cnt++ ] = c ;
}
number[ cnt ] = '\0'; /* terminate string */
intVal = atoi( number );
pos = 0;
cnt = 0;

/* ... */
I have just think of an idea, it seems work, but I don't know whether this
method is stupid or not:

while ( ( c = fgetc( fold ) ) != '\n' )
{
/* save digit at C68 in int1 */
if ( pos == 68 && isdigit(c) )
{
int1 = c - '0';
}

/* save digit at C69 in int2 */
if ( pos == 69 && isdigit(c) )
{
int2 = c - '0';
}

/* save digit at C70 in int3: */
if ( pos == 70 && isdigit(c) )
{
int3 = c - '0';
integerVal= (int1*100) + (int2*10) + int3 );
}
++pos;
}


Well, it's not stupid, just coooomplicated... :-)
We can make this much simpler by composing the value 'on the fly' (but
again, we will forget about this code later):

int intVal;

/* ... */

intVal = 0;
while ( ( c = fgetc( fold ) ) != '\n' )
{
/* convert C68-70 to integer: */
if ( pos > 67 && pos < 71 && isdigit( c ) )
intVal = intVal * 10 + ( c - '0' );
}
pos = 0;

/* ... */

Note that this code has a major deficiency: it will convert things like
e.g. "4#2" to 42 without complaining at all!

However, all these approaches have flaws or are at least not very
elegant, so I have to repeat my suggestion from a previous post:

- read in a whole line
- extract/convert the portions you need
- repeat until you are done

To accomplish this, you should read about the following functions in
your C book or reference manual:

fgets, strlen, strchr, strstr, strtol, strtod,
the is* function family, sscanf, ...

And, as you seem to have some problems with the general concepts of
string and file handling in C (don't worry, we all had/have to go
through this), read about these in your C book too.

Next, I strongly recommend the c.l.c-faq, it contains a lot of useful
information (and includes some book recommendations, in case you do not
own one yet); the faq can be found at:

http://www.eskimo.com/~scs/C-faq/top.html

If you still run into problems with your code or get stuck somewhere,
just ask.

Finally, I've set the Followup-To of this post to a.c.l.l.c-c++, as it
seems to me to be the most appropriate group for this thread.

<SNIP>

Hope that helped a bit.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #13

P: n/a
Irrwahn Grausewitz wrote:

Robert B. Clark <ep****@3pynexf.pbz> wrote:
<SNIP>

It would be much easier to copy your file on a line rather than character
basis. You would set a flag before beginning the copy, and have your code
check this flag before writing a line to the destination file. If it is
set, clear the flag and skip writing that line to the destination file.
Otherwise, write that line to the destination file.

/* Copy lines from old to new file.
Ignore first line from old file (firstline != 0).
*/

int retval = 0;
int firstline = 1;
char tempString[LINELEN];

/* .. */

while (fgets(tempString, LINELEN, fold))
{
if (firstline) /* Skip first line */
{
firstline = 0;
continue;
}


Nice try, but how do know you have read the /entire/ first line?
It may well consist of more than LINELEN-1 characters. What about:

if ( firstline ) /* Skip first line */
{
if ( strchr( tempString, '\n' ) )
firstline = 0;
continue;
}


Why do anything of that nature.

/* skip first line */
while (EOF != (ch = getchar()) && ('\n' != ch)) continue;

/* exit if less than 2 lines available */
if (EOF == ch) exit(0);

/* Now read the remainder of the file */
line = 2; column = 0;
while (EOF != (ch = getchar())) {
/* we have something in hand */
if ('\n' != ch) column++;
else {
column = 0;
line++;
}
if (COLWANTED == column) && (MAGIC == ch) {
/* do something special */
}
/* do something such as write to new file */
putc(ch, stdout);
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #14

P: n/a
Default User <fi********@boeing.com.invalid> scribbled the following
on comp.lang.c:
FrancisC wrote:
[gigantic snip]
If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I insert
the code?

[another big snip] Please trim down the quoted material to a minimal subset that is
necessary for your reply.


But Default, surely you must know that I have a 6-line signature.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
Nov 13 '05 #15

P: n/a
[Followups set to comp.lang.c]

Joona I Palaste wrote:
Default User <fi********@boeing.com.invalid> scribbled the following
on comp.lang.c:
FrancisC wrote:
[gigantic snip]

If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I
insert the code?

[another big snip]

Please trim down the quoted material to a minimal subset that is
necessary for your reply.


But Default, surely you must know that I have a 6-line signature.


Usenet guidelines suggest a maximum of four lines, as you already know. You
could lose the nationalist political comment and the lemon tree stuff, and
fit the rest onto four lines easily. That you do not do this is a puzzle to
those of us who know you to be an otherwise responsible netizen.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #16

P: n/a
Richard Heathfield <do******@address.co.uk.invalid> scribbled the following
on comp.lang.c:
[Followups set to comp.lang.c]
Joona I Palaste wrote:
Default User <fi********@boeing.com.invalid> scribbled the following
on comp.lang.c:
FrancisC wrote:
[gigantic snip]
If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I
insert the code?

[another big snip]

Please trim down the quoted material to a minimal subset that is
necessary for your reply.


But Default, surely you must know that I have a 6-line signature.

Usenet guidelines suggest a maximum of four lines, as you already know. You
could lose the nationalist political comment and the lemon tree stuff, and
fit the rest onto four lines easily. That you do not do this is a puzzle to
those of us who know you to be an otherwise responsible netizen.


Because you Richard are the first one to comment on this matter civilly,
I have abided by popular request and trimmed my signature down to four
lines.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We're women. We've got double standards to live up to."
- Ally McBeal
Nov 13 '05 #17

P: n/a
Joona I Palaste wrote:
.... snip ...
Because you Richard are the first one to comment on this matter
civilly, I have abided by popular request and trimmed my signature
down to four lines.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We're women. We've got double standards to live up to."
- Ally McBeal


Excellent. Thank-you.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #18

P: n/a
In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Richard Heathfield <do******@address.co.uk.invalid> scribbled the following
on comp.lang.c:
Usenet guidelines suggest a maximum of four lines, as you already know. You
could lose the nationalist political comment and the lemon tree stuff, and
fit the rest onto four lines easily. That you do not do this is a puzzle to
those of us who know you to be an otherwise responsible netizen.


Because you Richard are the first one to comment on this matter civilly,
I have abided by popular request and trimmed my signature down to four
lines.


Which still denotes a childish mentality: a mature person's decision to
follow or not the common rule has nothing to do with the nature of other
people's comments on the issue. You do it because YOU consider that it
is the right thing or you don't do it because YOU disagree with it.

In the case of the 4-line limit for the sig-block, there are good
arguments both pro and con. The main reason that prompted it is no
longer as valid today as it was back then: bandwidth saving. Few people
access Usenet today via 2400 bps modems, while 20 years ago such modems
were the very backbone of Usenet.

OTOH, it is still true that, in most cases, 4 lines provide ample space
for supplying as much contact information as needed/desired, so there is
little point in using larger sig-blocks. However, if someone really needs
more, there is no good reason for treating 4 lines as a hard limit.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #19

P: n/a
Dan Pop <Da*****@cern.ch> scribbled the following:
Which still denotes a childish mentality: a mature person's decision to
follow or not the common rule has nothing to do with the nature of other
people's comments on the issue. You do it because YOU consider that it
is the right thing or you don't do it because YOU disagree with it. In the case of the 4-line limit for the sig-block, there are good
arguments both pro and con. The main reason that prompted it is no
longer as valid today as it was back then: bandwidth saving. Few people
access Usenet today via 2400 bps modems, while 20 years ago such modems
were the very backbone of Usenet. OTOH, it is still true that, in most cases, 4 lines provide ample space
for supplying as much contact information as needed/desired, so there is
little point in using larger sig-blocks. However, if someone really needs
more, there is no good reason for treating 4 lines as a hard limit.


That's a good argument, Dan, but I'm sticking with my stripped-down sig
for the moment. As you can see, I'm *NOT* treating the 4-line limit as
set in stone, as sometimes the quotes tin generates underneath the name
block in the sig can cause it to grow to 5 lines. (It used to grow to 7
lines back when it was longer.)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
Nov 13 '05 #20

P: n/a
Joona I Palaste wrote:

Default User <fi********@boeing.com.invalid> scribbled the following
on comp.lang.c:
FrancisC wrote:
[gigantic snip]

If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I insert
the code?

[another big snip]

Please trim down the quoted material to a minimal subset that is
necessary for your reply.


But Default, surely you must know that I have a 6-line signature.

I'm not sure what your point is, as my message wasn't originally
directed towards you. I also find it puzzling to brag about netiquette
violations. What was the point of your message? My point was that it was
very difficult to read the one I refered to, because of the lack of
proper snippage.

Brian Rodenborn
Nov 13 '05 #21

P: n/a
Default User <fi********@boeing.com.invalid> scribbled the following
on comp.lang.c:
Joona I Palaste wrote:
Default User <fi********@boeing.com.invalid> scribbled the following
on comp.lang.c:
> FrancisC wrote:
> [gigantic snip]
>> If I want to copy the 70th character of the second line, and store it as
>> integer(eg, "6" to 6) for later writing to a new file, then how can I insert
>> the code?

> [another big snip]

> Please trim down the quoted material to a minimal subset that is
> necessary for your reply.


But Default, surely you must know that I have a 6-line signature.

I'm not sure what your point is, as my message wasn't originally
directed towards you. I also find it puzzling to brag about netiquette
violations. What was the point of your message? My point was that it was
very difficult to read the one I refered to, because of the lack of
proper snippage.


I was not bragging about anything. It was a subtle hint towards another
newsgroup, where the fact that I {have,had} a 6-line signature
seem{s,ed} to justify any breach of netiquette from anyone.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson
Nov 13 '05 #22

P: n/a
Dan Pop wrote:
.... snip ...
In the case of the 4-line limit for the sig-block, there are good
arguments both pro and con. The main reason that prompted it is
no longer as valid today as it was back then: bandwidth saving.
Few people access Usenet today via 2400 bps modems, while 20
years ago such modems were the very backbone of Usenet.

OTOH, it is still true that, in most cases, 4 lines provide ample
space for supplying as much contact information as needed/desired,
so there is little point in using larger sig-blocks. However, if
someone really needs more, there is no good reason for treating 4
lines as a hard limit.


Which is why nobody has objected to his 5 liner before. However
the existence of the limit allows us to berate those who take up a
page. Just as we should object to lack of snippage. The worst
offenders are those who attach a large sig without a proper sig
marker, and do it in HTML.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #23

P: n/a
Joona I Palaste wrote:
I was not bragging about anything. It was a subtle hint towards another
newsgroup, where the fact that I {have,had} a 6-line signature
seem{s,ed} to justify any breach of netiquette from anyone.

I have enough problem with stuff without deciphering coded messages to
other groups ;)

Brian Rodenborn
Nov 13 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.