NEWBIE: paste data, string-problem 
Author Message
 NEWBIE: paste data, string-problem

Hello NG,

I have got a file that contains some data. The file has e.g. 5 lines.
At the end of each line is a Carriage Return (<CR>).

Now I want to paste other data at the end of the first line, without
destroying the data in the other lines. I set the position-pointer to
the right position and write the new data to the first line. But the
data in the next line will be overwritten.

What can I do? Below is the sourcecode.

The content of the file "testfile.txt" e.g.:

test<CR>
hello<CR>

After I started the program, the file looks like this:

test newdata<CR>

What I want:

test newdata<CR>
hello<CR>

I tried also fopen("..","a+") but this does not work, it only appends
data at the end of the file, despite the position-pointer is set to
another location.

I am using Borland TC V2.01.

Thank you for your help in advance,
Stefan

___

Sourcecode:

#include <stdio.h>

void main(void)
{
        FILE *sp;
        int ch=0;

        sp=fopen("testfile.txt","r+");

        while (ch!=10)  /* carriage return */
        {
                ch=fgetc(sp);
        }

        fseek(sp, -2, SEEK_CUR);        /* set position-pointer */
        fprintf(sp, " newdata\n");

        fclose(sp);
        exit(0);

Quote:
}

Sent via Deja.com http://www.*-*-*.com/
Before you buy.


Sat, 29 Mar 2003 03:00:00 GMT  
 NEWBIE: paste data, string-problem

Quote:

>Hello NG,

>I have got a file that contains some data. The file has e.g. 5 lines.
>At the end of each line is a Carriage Return (<CR>).

>Now I want to paste other data at the end of the first line, without
>destroying the data in the other lines. I set the position-pointer to
>the right position and write the new data to the first line. But the
>data in the next line will be overwritten.

>What can I do?

Read section 19.14 of the C FAQ at
<URL:http://www.eskimo.com/~scs/C-faq/q19.14.html>. Read also the
sections referred to from section 19.14.

[cut]

Quote:
>#include <stdio.h>

>void main(void)

Always use 'int' as the return type of 'main' (and return a zero).

Quote:
>{
>    FILE *sp;
>    int ch=0;

>    sp=fopen("testfile.txt","r+");

Remenber to check for successful 'fopen' here.

Quote:

>    while (ch!=10)  /* carriage return */

It would be quite a lot more portable to compare with '\n' instead of
with 10.

Quote:
>    {
>            ch=fgetc(sp);
>    }

What happens if you reach EOF in your loop (i.e. if the file contains
no newline).

Quote:

>    fseek(sp, -2, SEEK_CUR);        /* set position-pointer */
>    fprintf(sp, " newdata\n");

>    fclose(sp);
>    exit(0);

Use "return 0;" instead.

Quote:
>}

/A

--
Andreas K?h?ri,
Uppsala University, Sweden.
=============================={ "free", as in "software" --> www.gnu.org



Sat, 29 Mar 2003 03:00:00 GMT  
 NEWBIE: paste data, string-problem

Quote:
> Date: Tue, 10 Oct 2000 10:43:44 GMT

> Newsgroups: comp.lang.c
> Subject: NEWBIE: paste data, string-problem

> Hello NG,

> I have got a file that contains some data. The file has e.g. 5 lines.
> At the end of each line is a Carriage Return (<CR>).

> Now I want to paste other data at the end of the first line, without
> destroying the data in the other lines. I set the position-pointer to
> the right position and write the new data to the first line. But the
> data in the next line will be overwritten.

> What can I do? Below is the sourcecode.

> The content of the file "testfile.txt" e.g.:

> test<CR>
> hello<CR>

> After I started the program, the file looks like this:

> test newdata<CR>

> What I want:

> test newdata<CR>
> hello<CR>

> I tried also fopen("..","a+") but this does not work, it only appends
> data at the end of the file, despite the position-pointer is set to
> another location.

You're pretty much stuck with rewriting the whole {*filter*}y file.
Sorry.
One method is to figure out the size of the file
(there are several ways, you can use fseek to SEEK_END and ftell,
not the best method.)
make a char array (buffer) that big (hint, use malloc)

read your file into the buffer
do all of your editing to the buffer
when you're ready, write the buffer over top of the old file.

Quote:
> Sent via Deja.com http://www.*-*-*.com/
> Before you buy.

--
Newton's Fourth Law:  Every action has an equal and opposite satisfaction.


Tue, 08 Apr 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. newbie needs help to strings, strings, strings

2. Copy Paste of class data

3. Newbie: Cut, Copy & Paste

4. Creating clipboard data for pasting a DDE link

5. scanf problem (DATA/MEMORY CONCEPT) (newbie)

6. Pasting into RichTextBox - formatting taxt pasted as text!

7. newbie - stuck on another string problem

8. Newbie Problem - Strings

9. newbie problem with a string

10. Newbie question: Strings and string manipulation.

11. RTF copy & paste problem

12. Problem with pasting text from Internet Explorer into Application

 

 
Powered by phpBB® Forum Software