473,290 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,290 software developers and data experts.

ignore a line

#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
23 4048
"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

"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
"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
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

"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
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
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
"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
"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
<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
> <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
"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
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
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
[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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Luis | last post by:
Please help, this is due at 11:59 PM tonite. Thanks Write a program that reads a person's name from the keyboard in the format First Middle Last. It should then (1) print each of the names on...
9
by: red | last post by:
If I have <span>text</span> <br> and then some text isn't there sopposed to be a line break between the span and the text ? Why do browsers ignore the <br> ?
5
by: Chris Mantoulidis | last post by:
Let's say I have this: std::string s1; std::cin >> s1; This will read s1 from cin until it finds a space (or a newline, whichever comes first). Okay this works. But when I want to continue...
6
by: Vandana Rola | last post by:
Hello Everyone, I posted this question earlier under creating Multiple choice quiz. Is it possible to ignore something using javascript. What I am trying to do is creating a multiple answer...
3
by: juli jul | last post by:
Hello, How can I read xml file but ignore the empty lines in it : is there some kind of function in C# that can read everything except for the empty lines? Thanks a lot! *** Sent via...
5
by: ma740988 | last post by:
Consider the source: # include <iostream> # include <string> # include <fstream> # include <vector> # include <sstream> using namespace std;
1
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
I want to match some HTML string using Regex but the linebreaks are getting me. Is there a way to just completely ignore linebreaks in my regular expression? If not, how would I specify a...
6
by: =?Utf-8?B?YXByMDUyNA==?= | last post by:
I am wondering if there is a devenv switch that will ignore build dependencies that are set in .xxproj files. At the command line I am attempting to build individual projects that I do not want...
13
by: youngjin.michael | last post by:
Hello, I am new to Python and have one simple question to which I cannot find a satisfactory solution. I want to read text line-by-line from a text file, but want to ignore only the first line....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.