-1

I want to go to to specific line no of a new text file and write some data on that line number. Like for example my data is 23 51 62

i want line 3 to have 23 line 1 to have 51 line 2 to have 62 and so on How do i jump to different line number for every data

    void main()

        {
            int i=0;
            FILE *fconfig;
            fconfig = fopen("config_new.txt","w");

        for ( i = 0; i < 5; i++)
        {
            /* code */
            if(fconfig == NULL)
            {
                printf("error!\n");
                exit(1);
            }

            // scanf("%d",&num);
            if(i%2 == 0)
            {
            fseek(fconfig,0+i,0);
            fprintf(fconfig, "%d\n", i+17 );
            }
        }
            // fclose(fconfig);

        }

i want txt file to have data on even line numbers and \n on others.

This doesen't seem to work. What am i missing here.

nincompoop_
  • 126
  • 5

2 Answers2

0

You write that the file is new - then it it will be simple to write n-1 blank lines first (error checking omitted ...):

void write_line(const char * fname, const char * line, size_t blank_lines) {
    FILE stream = fopen(fname, "w");

    for (size_t i=0; i < blank_lines; i++) 
        fputs("\n", stream);

    fprintf(stream, "%s\n", line);          
    fclose(stream);
}

But - this seems like a somewhat weird function?

user422005
  • 1,989
  • 16
  • 34
  • But if i write n-1 blank lines first i might have to again go back and and rewrite those lines. Will that work. – nincompoop_ Dec 26 '18 at 07:44
  • https://stackoverflow.com/questions/4652436/c-file-streams-appending-at-the-beginning you need to rewrite all the lines that come after the line you want to insert if you want to insert a line in the middle. – KamilCuk Dec 27 '18 at 05:06
0

As explained by Andrew below, fseek sets the position indicator associated with the stream to a new position. Check the reference. http://www.cplusplus.com/reference/cstdio/fseek/

Reproducing the example in the above reference

#include <stdio.h>

int main ()
{
  FILE * pFile;
  pFile = fopen ( "example.txt" , "wb" );
  fputs ( "This is an apple." , pFile );
  fseek ( pFile , 9 , SEEK_SET );
  fputs ( " sam" , pFile );
  fclose ( pFile );
  return 0;
}
// After this code is successfully executed, the file example.txt contains: 
// This is a sample.

For the problem you mentioned, simply writing into the file from beginning to end will suffice.

Get hold of a good book on C programming. For now,

    #include <stdio.h>
    #include <stdlib.h>

int main()
{
    int i;

    FILE *fconfig = fopen("config_new.txt","w");
    if(fconfig == NULL)
    {
        perror("Error!\n"); // print error message
        exit(1);
    }

    for (i = 0; i < 5; i++) // line numbers from 0 ? 
    {
            if(i % 2 == 0)
            {
                fprintf(fconfig, "%d\n", i+17 );
            }
            else 
            {
                fprintf(fconfig, "\n" ); // blank lines on odd numbered lines
            }

    }
    fclose( fconfig ); // close the file after you finish writing.
    exit(0);
}
shr
  • 875
  • 10
  • 20
  • *fseek allows you to go to a location within an existing file. It is useful when the file has fixed size records or you know in advance the location of byte you want to go to.* Not in general. `fseek()` is only useful to *return* to a location in a text stream. Per [**7.21.9.2 The fseek function**, paragraph 4 of the C standard](https://port70.net/~nsz/c/c11/n1570.html#7.21.9.2p4): "For a text stream, either offset shall be zero, or **offset shall be a value returned by an earlier successful call to the ftell function** on a stream associated with the same file and whence shall be SEEK_SET." – Andrew Henle Dec 26 '18 at 15:55