Hey! I'm new to the forum and also a new developer.

I'm trying to build an numerical solution for an equoation and my parameters are in an ascii file where I already know their position

The file is structured like the example:

#Parameter 1
parameter value
#Parameter 2
parameter value

I'm interested in a code that reads ONLY parameter value files and input them to a variable. E.g.(pseudocode)

read_line(2)
input line(2) to the variable.

Any help? Thanks in advance

Unless the lines have a fixed column width, you have little choice but to read and discard lines until reaching a certain count:

#include <stdio.h>

/*
    Get the nth line (1-based) from the current read position
*/
int get_nth_line(FILE *in, char *buf, size_t size, size_t n)
{
    size_t i = 0;

    while (i < n && fgets(buf, size, in) != NULL)
        ++i;

    return i;
}
#include <stdio.h>

int main(void)
{
    FILE *in = fopen("file", "r");
    char buf[BUFSIZ];

    if (get_nth_line(in, buf, sizeof buf, 2) == 2)
        puts(buf);

    return 0;
}

Until now, it seems working! An easy solution. But combining with atof/atoi/atol set to input the right parameters.

For multiple lines it doesn't work. I've got to empty the buffer maybe. How can I do this?

For multiple lines it doesn't work.

Two things:

  1. My code wasn't meant to be a full solution to your problem, only a push in the right direction.
  2. Please elaborate on how it doesn't work.

Two things:

  1. My code wasn't meant to be a full solution to your problem, only a push in the right direction.
  2. Please elaborate on how it doesn't work.

When I want to read line 4 and expecting the corrected data to be input, all fields are then zero It's like reading only the first line I want to read and then fills the buffer with the new lines

I went ahead and test my code. It works perfectly as designed. Could you post your attempt so I can see what you're doing differently?

I'll check again. Maybe I was doing something wrong. Thanks a lot for your help

So, i need to use your routine for at least 12 times. The first time I use it (sequentialy), extracts me the proper number.But after that only zeros appear.

if (get_nth_line(in, buf, sizeof buf, 8) == 8)
	{
	puts(buf);
	width=atof(buf);
	}	
	if (get_nth_line(in, buf, sizeof buf, 28) == 28)
	{
	puts(buf);
	steps=atoi(buf);
	}

	if (get_nth_line(in, buf, sizeof buf, 10) == 10)
	{
	puts(buf);
	height=atof(buf);
	}
	if (get_nth_line(in, buf, sizeof buf, 5) == 5)
	{
	puts(buf);
	trulength=atof(buf);
	}
	if (get_nth_line(in, buf, sizeof buf, 29) == 29)
	{
	puts(buf);
	modulus=atoi(buf);
	}
	if (get_nth_line(in, buf, sizeof buf, 36) == 36)
	{
	puts(buf);
	frequency=atof(buf);
	}
	if (get_nth_line(in, buf, sizeof buf, 38) == 38)
	{
	puts(buf);
	control=atoi(buf);
	}
	if (get_nth_line(in, buf, sizeof buf, 31) == 31)
	{
	puts(buf);
	outopt=atoi(buf);
	}

So I think we've got to find a way to empty the buffer

You're probably walking off the end of the file. Note that the line count you're passing in is relative to wherever you are in the file. So if there are five lines in the file and you've already read two of them, passing 5 in will fail.

Let's say I wanted to read every other line with get_nth_line(). The call would look like this:

int main(void)
{
    FILE *in = fopen("test.txt", "r");
    char buf[BUFSIZ];
 
    while (get_nth_line(in, buf, sizeof buf, 2) == 2)
        puts(buf);
 
    return 0;
}

It works because get_nth_line doesn't rewind the file stream each time, it starts where it left off. Adjust your calls accordingly, or post a sample of your file and I'll help you.

I'll adjust the calls in the appropiate way!Thanks again!

Working perfect!As I wanted!One more problem though...it still output the values of what the routine reads. I think this is the way puts works...

This article has been dead for over six months. Start a new discussion instead.