How to print the penultimate integer ? "C"

How to get last but one if user choose a number from a list in a file.txt ?
I have this example:

1
2
3
4
15000 560 100
16500 500 300
17150 200 360
18144 221 130


If user choose for example 17150 I want to print out the number before which is 16500
Last edited on
For example:
token = ""
read token
current = ""
previous = ""
WHILE read current
  IF current does match token
  THEN print previous
  previous = current
I coded it in C++ - before I realised you want C. Ahhhhh. Dohhhh. Here's my C++ code using a vector that contains all the data so that multiple requests can be made without re-reading the file. I'll do a C one shortly.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

auto getPrev(const std::vector<int>& vi, int num)
{
	const auto ret {std::find(vi.begin(), vi.end(), num)};

	if (ret == vi.end() || ret == vi.begin())
		return vi.end();

	return std::prev(ret);
}

int main()
{
	std::ifstream ifs("file.txt");

	if (!ifs.is_open()) {
		std::cout << "Cannot open file\n";
		return 1;
	}

	std::vector<int> vi;
	int id {};

	for (std::string line; ifs >> id && getline(ifs, line); vi.push_back(id));

	std::cout << "Enter number to find: ";
	std::cin >> id;

	const auto previd {getPrev(vi, id)};

	if (previd != vi.end())
		std::cout << "Previous is: " << *previd << '\n';
	else
		std::cout << "No previous\n";
}

OK, for C - but needs to re-read the file every time you want to know the previous.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdlib.h>
#include <stdio.h>

int main()
{
	FILE* ifs = fopen("file.txt", "r");

	if (ifs == NULL) {
		puts("Cannot open file");
		return 1;
	}

	int id = 0;

	printf("Enter id to find: ");
	scanf("%d", &id);

	int lastid = -1, fid = 0;
	bool found = false;

	for (char buff[256] = {0}; fscanf(ifs, "%d", &fid) != EOF && fgets(buff, 255, ifs) != NULL; lastid = fid) {
		if (fid == id && lastid != -1) {
			found = true;
			break;
		}
	}

	if (found == true)
		printf("Previous is: %d\n", lastid);
	else
		puts("No previous");
}

Yesss... that is working.. I can use it to parse the lastid in to my program. My goal was to modify the data and then to compare with the previous one.. So one last question seeplus, is it possible to use the lastid as a string, because in my code as you already know.. same one you know, I try to improve it so I need to retrieve the data as string not integer ?
Last edited on
Of course.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
	FILE* ifs = fopen("file.txt", "r");

	if (ifs == NULL) {
		puts("Cannot open file");
		return 1;
	}

	char id[6] = {};

	printf("Enter id to find: ");
	scanf("%5s", id);

	char lastid[6] = {}, fid[6] = {};
	bool found = false;

	for (char buff[256] = {0}; fscanf(ifs, "%5s", fid) != EOF && fgets(buff, 255, ifs) != NULL; strcpy(lastid, fid)) {
		if (strcmp(fid, id) == 0 && *lastid != 0) {
			found = true;
			break;
		}
	}

	if (found == true)
		printf("Previous is: %s\n", lastid);
	else
		puts("No previous");
}

Ohh.. thank you seeplus that helped me alot. I adapted to my code with some modifies and its working just fine..

Until next time.. cheers.. :D
Topic archived. No new replies allowed.