C - reading ints and chars into arrays from a file - c

I have a .txt file with values written in this format: LetterNumber, LetterNumber, LetterNumber etc (example: A1, C8, R43, A298, B4). I want to read the letters and the numbers into two separate arrays (example: array1 would be A C R A B; array2 would be 1 8 43 298 4). How can I make it happen?
At the moment I only figured out how to read all the values, both numbers and letters and the commas and everything, into one array of chars:
FILE *myfile;
myfile = fopen("input1.txt", "r");
char input[677]; //I know there are 676 characters in my .txt file
int i;
if (myfile == NULL) {
printf("Error Reading File\n");
exit (0);
}
for (i=0; i<677; i++) {
fscanf(myfile, "%c", &input[i]);
}
fclose(myfile);
But ideally I want two arrays: one containing only letters and one containing only numbers. Is it even possible?
I would appreciate any kind of help, even just a hint. Thank you!

Define another array for integers,
int inputD[677];
Then in for loop read one char, one integer and one space char at a time.
fscanf(myfile, " %c%d %*[,] ", &input[i], &inputD[i]);

I would actually define a struct to keep letter and number together; the data format strongly suggests that they have a close relation. Here is a program that exemplifies the idea.
The scanf format is somewhat tricky to get right (meaning as simple as possible, but no simpler). RoadRunner, for example, forgot to skip whitespace preceding the letter in his answer.
It helps that we have (I assume) only single letters. It is helpful to remember that all standard formats except %c skip whitespace. (Both parts of that sentence should be remembered.)
#include<stdio.h>
#define ARRLEN 10000
// Keep pairs of data together in one struct.
struct CharIntPair
{
char letter;
int number;
};
// test data. various space configurations
// char *data = " A1, B22 , C333,D4,E5 ,F6, Z12345";
void printParsedPairs(struct CharIntPair pairs[], int count)
{
printf("%d pairs:\n", count);
for(int i = 0; i<count; i++)
{
printf("Pair %6d. Letter: %-2c, number: %11d\n", i, pairs[i].letter, pairs[i].number);
}
}
int main()
{
setbuf(stdout, NULL);
setbuf(stdin, NULL);
// For the parsing results
struct CharIntPair pairs[ARRLEN];
//char dummy [80];
int parsedPairCount = 0;
for(parsedPairCount=0; parsedPairCount<ARRLEN; parsedPairCount++)
{
// The format explained>
// -- " ": skips any optional whitespace
// -- "%c": reads the next single character
// -- "%d": expects and reads a number after optional whitespace
// (the %d format, like all standard formats except %c,
// skips whitespace).
// -- " ": reads and discards optional whitespace
// -- ",": expects, reads and discards a comma.
// The position after this scanf returns with 2 will be
// before optional whitespace and the next letter-number pair.
int numRead
= scanf(" %c%d ,",
&pairs[parsedPairCount].letter,
&pairs[parsedPairCount].number);
//printf("scanf returned %d\n", numRead);
//printf("dummy was ->%s<-\n", dummy);
if(numRead < 0) // IO error or, more likely, EOF. Inspect errno to tell.
{
printf("scanf returned %d\n", numRead);
break;
}
else if(numRead == 0)
{
printf("scanf returned %d\n", numRead);
printf("Data format problem: No character? How weird is that...\n");
break;
}
else if(numRead == 1)
{
printf("scanf returned %d\n", numRead);
printf("Data format problem: No number after first non-whitespace character ->%c<- (ASCII %d).\n",
pairs[parsedPairCount].letter, (int)pairs[parsedPairCount].letter);
break;
}
// It's 2; we have parsed a pair.
else
{
printf("Parsed pair %6d. Letter: %-2c, number: %11d\n", parsedPairCount,
pairs[parsedPairCount].letter, pairs[parsedPairCount].number);
}
}
printf("parsed pair count: %d\n", parsedPairCount);
printParsedPairs(pairs, parsedPairCount);
}
I was struggling a bit with my cygwin environment with bash and mintty on a Windows 8. The %c would sometimes encounter a newline (ASCII 10) which should be eaten by the preceding whitespace-eating space, derailing the parsing. (More robust parsing would, after an error, try to read char by char until the next comma is encountered, and try to recover from there.)
This happened when I typed Ctr-D (or, I think, also Ctr-Z in a console window) in an attempt to signal EOF; the following enter key stroke would cause a newline to "reach" the %c. Of course text I/O in a POSIX emulation on a Windows system is tricky; I must assume that somewhere between translating CR-NL sequences back and forth this bug slips in. On a linux system via ssh/putty it works as expected.

You basically just have to create one char array and one int array, then use fscanf to read the values from the file stream.
For simplicity, using a while loop in this case makes the job easier, as you can read the 2 values returned from fscanf until EOF.
Something like this is the right idea:
#include <stdio.h>
#include <stdlib.h>
// Wasn't really sure what the buffer size should be, it's up to you.
#define MAXSIZE 677
int
main(void) {
FILE *myFile;
char letters[MAXSIZE];
int numbers[MAXSIZE], count = 0, i;
myFile = fopen("input1.txt", "r");
if (myFile == NULL) {
fprintf(stderr, "%s\n", "Error reading file\n");
exit(EXIT_FAILURE);
}
while (fscanf(myFile, " %c%d ,", &letters[count], &numbers[count]) == 2) {
count++;
}
for (i = 0; i < count; i++) {
printf("%c%d ", letters[i], numbers[i]);
}
printf("\n");
fclose(myFile);
return 0;
}

Related

How do I make sscanf ignore white spaces? C

I'm trying to read formatted data with sscanf in this format:
"%d,%d,%d"
for example,
sscanf(buffer, "%d,%d,%d", n1, n2, n3)
and it is critic that there is no white space between every number, because the input comes like this, for example:
109,304,249
194,204,482
etc.
But when I give sscanf an input like:
13, 56, 89
145, 646, 75
it still reads it even though it is not in the specifed format, and should be invalid
Is there is a way to write something such that sscanf will work as I expect it to work?
scanf ignores leading whitespace for most conversions, but what you want is the opposite of ignoring whitespace.
You cannot tell scanf to error out on a whitespace, but you can detect how many whitespace characters were consumed. For example:
#include <stdio.h>
int main()
{
int first, second, before, after;
int nread;
nread = scanf("%d,%n %n%d", &first, &before, &after, &second);
if (nread != 2) {
printf ("Error in the input stream, 2 items expected, %d matched\n", nread);
}
else if (before != after) {
printf ("Error in the input stream, %d whitespace characters detected\n",
after-before);
}
else {
printf ("Got numbers %d %d\n", first, second);
}
}
This detects whitespace before the second input.
Having said that, erroring out on whitespace is probably not a good idea.
The %d conversion format ignores leading white space as well as an optional + sign. If you want a stricter validation, you can:
use an extra validation phase before or after converting the values.
use a hand coded conversion that rejects malformed input.
Here are some propositions:
#include <stdio.h>
#include <string.h>
int main() {
char input[100];
char verif[100];
int a, b, c;
if (fgets(input, sizeof input, stdin)) {
if (sscanf(input, "%d,%d,%d", &a, &b, &c) == 3) {
snprintf(verif, sizeof verif, "%d,%d,%d\n", a, b, c);
if (strcmp(input, verif) == 0) {
printf("input is valid, a=%d, b=%d, c=%d\n", a, b, c);
return 0;
} else {
printf("input contains extra characters\n");
return 1;
}
} else {
printf("input does not match the pattern\n");
return 1;
}
} else {
printf("no input\n");
return 1;
}
}
You could just check if the input string contains any white space:
if (input[strcspn(input, " \t")] != '\0') {
printf("input string contains spaces or TABs\n");
}
But negative values, redundant + signs and leading zeroes will pass the test (eg: "-1,+1,+0001").
If all values must be positive, you could use scanf() to perform a poor man's pattern matching:
if (fgets(input, sizeof input, stdin)) {
char c1, c2;
if (sscanf(input, "%*[0-9],%*[0-9],%*[0-9]%c%c", &c1, &c2) == 1 && c1 == '\n') {
/* input line contains exactly 3 numbers separated by a `,` */
} else {
printf("invalid format\n");
}
}
Note however these remarks:
redundant leading zeroes would still be accepted by this validation phase (eg: "00,01,02").
overlong numbers will technically cause undefined behavior in all of the above methods (eg: "0,0,999999999999999999999999999999"), But the first approach will detect this problem if the conversion just truncates or maximises the converted value.
an overlong input line (longer than 98 bytes plus a newline) will be truncated by fgets(), leaving the rest of the input for the next read operation. This may be a problem too.
The solution to your problem depends on how strict and concise the validation must be.
One idea to validate the input is to use the regular expression to match three integers delimited by commas without including other characters.
#include <stdio.h>
#include <regex.h>
#include <string.h>
int main()
{
regex_t preg; // regex pattern
int ret;
int n1, n2, n3;
char *p; // pointer to a character th the string
char buffer[BUFSIZ]; // input buffer
// compile regex to match three integers delimited by commas
ret = regcomp(&preg, "^([+-]?[[:digit:]]+,){2}[+-]?[[:digit:]]+$", REG_EXTENDED);
if (ret) {
fprintf(stderr, "Compile error of regex\n");
exit(1);
}
while (fgets(buffer, BUFSIZ, stdin)) {
if ((p = rindex(buffer, '\n'))) *p = 0; // remove trailing newline
ret = regexec(&preg, buffer, 0, NULL, 0);
if (!ret) { // input matches the regex pattern
sscanf(buffer, "%d,%d,%d", &n1, &n2, &n3);
printf("=> %d,%d,%d\n", n1, n2, n3);
} else {
printf("Error in the input: %s\n", buffer);
}
}
return 0;
}
where ^([+-]?[[:digit:]]+,){2}[+-]?[[:digit:]]+$ is the regex:
^ anchors the start of the input string.
[+-]? matches zero or one leading plus or minus sign.
[[:digit:]]+, matches the sequence of digits followed by a comma.
The parentheses ( pattern ) makes a group of the pattern.
The quantifier {2} specifies the number of repetition of the previous atom (including group).
$ anchors the end of the input string.

Program that prints specific characters from a line in a file

This is a programming problem i stumbled upon. It requires some specific stuff to be done and that is the reason that i copy files etc. The problem is that it requires that i print out the last character from onoma, the first character from epithetoand the product of the four digits of afm. The thing is that it must be done by reading the file. My code is the following:
#include <stdio.h>
main()
{
FILE *fp, *fp2;
char ch;
char onoma[12];
char epitheto[25];
char afm[4];
char adt[8];
fp = fopen("C:\\Users\\kostikas\\Desktop\\New folder\\myfile.txt", "w");
printf("Dose onoma: \n");
scanf("%s", &onoma);
printf("Dose epitheto: \n");
scanf("%s", &epitheto);
printf("Dose afm: \n");
scanf("%s", &afm);
printf("Dose adt: \n");
scanf("%s", &adt);
fprintf(fp, "%s \n%s \n%s \n%s \n", onoma, epitheto, afm, adt);
fclose(fp);
fp = fopen("C:\\Users\\kostikas\\Desktop\\New folder\\myfile.txt", "r");
fp2 = fopen("C:\\Users\\kostikas\\Desktop\\New folder\\myfile2.txt", "w");
while((ch = fgetc(fp)) != EOF)
{
fputc(ch, fp2);
}
fclose(fp);
fclose(fp2);
fp2 = fopen("C:\\Users\\kostikas\\Desktop\\New folder\\myfile2.txt", "r");
}
I have absolutely no idea on how to continue from here. I'm utterly baffled. If my post is against community guidelines please let me know and i will delete it. Your help would be greatly appreciated though. Thank you.
This is a bunch of tasks smashed together, any one of which is a challenge to a beginner. Let's break this into parts.
The problem is that it requires that i
print the last character from onoma
print the first character from eponimo
print the product of the four digits of afm.
We'll focus on those one at a time.
The thing is that it must be done by reading the file.
And leave this for later, it just complicates everything. First get the logic working with hard coded values, then worry about where the input comes from.
print the first character from eponimo
Each character can be accessed with eponimo[n] starting at 0. First character is eponimo[0], then eponimo[1], and so on. Printing the first character is just...
char eponimo[] = "Basset hounds got long ears";
printf("%c\n", eponimo[0]);
We use %c, not %s, because it is a single character, not a string which is a pointer to an array of characters.
print the last character from onoma
The last character would be the length of the string, minus 1 because it starts from 0. You get the length of a string with strlen.
char onoma[] = "Oodles of poodles jump der strudel";
size_t onoma_length = strlen(onoma);
printf("%c\n", onoma[onoma_length-1]);
print the product of the four digits of afm.
This is a little trickier. It requires math, loops, and converting characters to integers.
First, check that afm is long enough, again using strlen.
if( strlen(afm) < 4 ) {
fprintf(stderr, "afm must have at least four digits\n");
exit(1);
}
You can also check if the characters are digits with isdigit.
Then we can loop through the first four characters.
char afm[] = "123456";
for( int i = 0; i < 4; i++ ) {
printf("%c\n", afm[i]);
}
Now we need to turn them into integers. Whole strings are done with atoi (ASCII to Integer), but we want single characters. The thing about characters in C is they're just integers and we can do math on them. 0 is 48, 1 is 49, and so on. We take advantage that all the integers are in a sequence.
char afm[] = "123456";
for( int i = 0; i < 4; i++ ) {
// '0' - '0' is 0.
int digit = afm[i] - '0';
printf("%d\n", digit);
}
Note that I've switched to %d for printing an integer (a "digit").
Now we multiply them.
char afm[] = "123456";
int product = 1; // start with 1, not 0, else it will always be 0
for( int i = 0; i < 4; i++ ) {
// '0' - '0' is 0.
int digit = afm[i] - '0';
product *= digit; // same as product = (product * digit)
}
printf("%d\n", product);
Now that you know how to do it with fixed strings, you can substitute what you've read from the file. First, open it for reading and check that it worked. This check will save you a lot of misery.
char filename[] = "C:\\Users\\kostikas\\Desktop\\New folder\\myfile.txt";
fp = fopen(filename, "r"); // open for reading
if( fp == NULL ) {
perror("Could not open input file");
}
Now allocate memory and read lines. scanf reads from "standard input", roughly what you tyope into the program. fscanf reads from a filehandle.
// Allocate space for 10 characters.
// Read only 9 because strings need an extra null character to indicate the end.
char onoma[10];
fscanf(fp, "%9s", onoma);
char eponimo[10];
fscanf(fp, "%9s", eponimo);
char afm[10];
fscanf(fp, "%9s", afm);
// We're done reading, close the file.
fclose(fp);
However, scanf and fscanf have a lot of surprising behaviors. And we need to preallocate our best guess of how much we're going to read and be careful not to read in more than we've allocated.
If at all possible, use getline instead. Most compilers support it. It will allocate memory for you. It doesn't have the caveats of fscanf, but it also doesn't strip the newline off the end like fscanf does.
size_t size = 0;
char *onoma = NULL;
getline(&onoma, &size, fp);
Writing is easier. Open the file for writing, you know how to do that, and use fprintf instead of printf, like fscanf instead of scanf.
fprintf(fp, "%c\n", eponimo[0]);
That should get you going. This exercise is throwing multiple problems at a beginner. Break them up into parts and tackle them one at a time.

I have problems with getting numbers from the file. Function fscanf reads only strings. 8

Fscanf does not work and I can't understand why. It only reads strings and nothing else.
1 2 3 is written in the file.txt.
Here's the code:
include<stdio.h>
int main()
{
FILE* ptr = fopen("file.txt","r");
if (ptr==NULL)
{
printf("no such file.");
return 0;
}
char* buf[100];
int a;
fscanf(ptr," %d ",a);
printf("%d\n", a);
fscanf(ptr," %s ",buf);
printf("%s\n", buf);
return 0;
}
There are several issues in your provided code, I would like to first talk about before getting to the answer you have asked for.
1.
fscanf(ptr," %d ",a);
This is false. Here the address of an int is needed as third argument. You need the ampersand operator & to access an address of a variable, like:
fscanf(ptr," %d ",&a);
2.
fscanf(ptr," %s ",buf);
Is also false. A pointer to a char array is needed here as third argument, but buf is declared as an array of 100 pointers after
char* buf[100];
You need to declare buf in the right way as a char array, like:
char buf[100];
to make it work with:
fscanf(ptr," %s ",buf);
3.
You have forgot the # in the include directive for stdio.h:
include<stdio.h>
Also, there should be a white space gap between #include and the file you want to include.
So the preprocessor directive should be look like:
#include <stdio.h>
4.
If the open stream operation fails you should not use return with a value of 0.
If an operation fails, that is crucial to the program itself, the return value of the program should be a non-zero value (the value of 1 is the most common) or EXIT_FAILURE(which is a macro designed for that purpose (defined in header <stdlib.h>)), not 0, which indicating to outer software applications as well as the operation system, that a problem has occurred and the program could not been executed successfully as it was ment for its original purpose.
So use:
if (ptr==NULL)
{
printf("no such file.");
return 1;
}
5.
Fscanf does not work and I can't understand why. It only reads strings and nothing else.
What did you expect as result? What do you want that fscanf()should do?
The format specifier %s is used to read strings until the first occurrence of a white space character in the input stream (skips leading white space characters until the matching sequence is encountered), pointed to by ptr.
Then I get from your header title:
I have problems with getting numbers from the file
that you want only the numbers from the file.
If you want to get the numbers only from the text file, you do not need the char array buf and the whole things with reading strings at all.
Simply use something like:
int a,b,c; // buffer integers.
fscanf(ptr,"%d %d %d",&a,&b,&c);
printf("%d %d %d\n",a,b,c);
Of course, this expressions implying that it only work with the given example of the 1 2 3 data or anything equivalent to (integer) (integer) (integer) but I think you get the idea of how it works.
And, of course, you can apply the scan operation by using fscanf() (and also the print operation by using printf()) for each integer separate in a loop, instead to scan/print all integers with just one call to fscanf() and printf(), f.e. like:
#define Integers_in_File 3
int array[Integers_in_File];
for(int i = 0; i < Integers_in_File; i++)
{
fscanf(ptr,"%d",&array[i]); // writing to respective int buffers,
} // provided as elements of an int array.
for(int i = 0; i < Integers_in_File; i++)
{
printf("%d",array[i]); // Output the scanned integers from
} // the file.
The whole program would be than either:
#include <stdio.h>
int main()
{
FILE* ptr = fopen("file.txt","r");
if (ptr==NULL)
{
printf("no such file.");
return 1;
}
int a,b,c; // buffer integers.
fscanf(ptr,"%d %d %d",&a,&b,&c);
printf("%d %d %d\n",a,b,c);
return 0;
}
Or that:
#include <stdio.h>
#define Integers_in_File 3
int main()
{
int array[Integers_in_File];
FILE* ptr = fopen("file.txt","r");
if (ptr==NULL)
{
printf("no such file.");
return 1;
}
for(int i = 0; i < Integers_in_File; i++)
{
fscanf(ptr," %d",&array[i]); // writing to respective intbuffers,
} // provided as elements of an int
// array.
for(int i = 0; i < Integers_in_File; i++)
{
printf("%d",array[i]); // Output the scanned integers from
} // the file.
return 0;
}
The variadic arguments to fscanf() must be pointers.
Whitespace is the default delimiter and need not be included in the format string.
If the input stream does not match the format specifier, the content remains buffered, and the argument is not assigned. You should therefore check the conversion which can fail due to mismatching content or EOF.
To declare an array for a character string, the array type must be char not char* - that would be an array of pointers, not an array of characters.
char buf[100];
int a;
if( fscanf( ptr, "%d", &a ) > 0 )
{
printf( "%d\n", a ) ;
if( fscanf(ptr, "%s", buf) > 0 )
{
printf( "%s\n", buf ) ;
}
}
Or simply:
char buf[100];
int a;
if( fscanf( ptr, "%d%s", &a, buff ) == 2 )
{
printf( "%d\n", a ) ;
printf( "%s\n", buf ) ;
}

Reading float numbers from a file in a special manner

I'm trying to read numbers from the file in a 2D-Array, i have to skip the first row and first column, rest all have to save in an array, i've tried using sscanf, fscanf and even strtok() but failed miserably. So please help me to solve this issue.
Thanx in advance,
Link to the file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]){
FILE *f=fopen("Monthly_Rainfall_Himachal.txt","r");
float data[12][12];
int i,j;
char newLine[1000];
fgets(newLine,1000,f);
char* item,waste;
i=0;
while(1)//read file line by line
{
fscanf(f, "%s %f %f %f %f %f %f %f %f %f %f %f %f ", waste, &data[i][0], &data[i][1], &data[i][2], &data[i][3], &data[i][4], &data[i][5], &data[i][6], &data[i][7], &data[i][8], &data[i][9], &data[i][10], &data[i][11]);
i++;
if(feof(f))break;
}
fclose(f);
for(i=0 ;i<12 ;i++){
for(j=0 ;j<12 ;j++){
printf("%.1f\t",data[i][j]);
}
printf("\n");
}
return 0;
}
Problems:
You don't check if the fopen was successful in opening the file or not and blindly assume it did.
Check its return value:
if(f == NULL)
{
fputs("fopen failed! Exiting...\n", stderr);
return EXIT_FAILURE;
}
Instead of reading and storing the first line, you can just read and discard it using scanf:
scanf("%*[^\r\n]"); /* Discard everything until a \r or \n */
scanf("%*c"); /* Discard the \r or \n as well */
/* You might wanna use the following instead of `scanf("%*c")`
if there would be more than one \r or \n
int c;
while((c = getchar()) != '\n' && c != '\r' && c != EOF);
But note that the next fscanf first uses a `%s` which
discards leading whitespace characters already. So, the
`scanf("%*c");` or the while `getchar` loop is optional
*/
You have an unused character pointer item and a character variable waste. Both of these are unnecessary. So, remove them.
In the very long fscanf line, you first try to scan in a string into a character variable which invokes Undefined Behavior and things go haywire. You also need to check its return value to see if it was successful.
Replace that fscanf line with the following:
if(fscanf(f, "%*s") == EOF)
{
fputs("End Of File! Exiting...\n", stderr);
return EXIT_SUCCESS;
}
for(j = 0; j < 12; j++)
{
if(fscanf(f, "%f", &data[i][j]) != 1)
{
fputs("End Of File or bad input! Exiting...\n", stderr);
return EXIT_SUCCESS;
}
}
You assume the input is of a maximum of 12 lines, but if it contains more than 12 lines, your code will invoke Undefined Behavior due to an array overrun.
Check the value of i along with the feof to make sure it does not go beyond 11:
if(i >= 12 || feof(f))
Note: I did not test any of the above code. Please correct me if I've made a mistake. Thanks!

fgets give me random big number

This is my code, I don't know how to use fgets after scanf so I am using fgets in the 26th line too but every time I use it, it give me big number(ex.2752100) but I write 2.
Why is it doing it?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char veta[100];
int line = 1;//tell what line it is on
int found = 0;
char a[100];//put the characters of the line into here
char b[100];
char linesearch[10];//the line you are looking for
FILE *file;//the file pointer
file = fopen("text.txt","r");//point the file
if (file == NULL)
{
printf("file does not exist or doesn't work\n");
return 0;
}
printf("Ahoj, naucim te psat snadno a rychle! \n");
printf("Vyber si uroven slozitosti od 1 do 10:\n");
//scanf("%d", &linesearch);
fgets(linesearch,10,stdin);
printf("\nHledam uroven %d ...\n\n",linesearch);
EDIT:
i have another problem:
while(fgets(a,100,file))
{
if(x == line)
{
found = 1;
printf("level %d found,level %d say: %s",x,x,a);
}
else
printf("reading level: %d\n",line );
line++;
}
printf("\nwrite your string as fast as you can!!");
fgets(veta,40,stdin);
if (strcmp(veta,a) == 0)
{
printf("\nwell done!!!!\n");
}
else
{
printf("\nwrong!!!!\n");
printf("%s", a);
printf("%s", veta);
}
i have small senteces(ex I like my mum and she likes me,etc) i want to compare my text with text from file and get answer if I write it well or not. Bonus points if it tell me how many mistakes i did it will be powerful!.
The fgets() function reads character data from the input. To convert this character data to an integer, use atoi() or a similar function.
fgets(linesearch, 10, stdin);
int x = atoi(linesearch);
printf("\nHledam uroven %d ...\n\n",x);
Your printf statement is printing out the address of the linesearch array, which will seem like a random big number.
If you want to read from stdin, into a char array, using scanf() and then print as an int:
scanf("%s", linesearch); // e.g. reads 1234 into array linesearch[].
printf(" %s ...\n\n",linesearch); // Prints string in array linesearch[].
printf(" %p ...\n\n",linesearch); // Prints base address of linesearch[].
int iNum = atoi(linesearch); // Converts string "1234" to number 1234.
printf(" %d ...\n\n",iNum); // Prints the converted int.
iNum++; // Can perform arithmetic on this converted int.
You are getting a big number from printf because you used %d in the format. The number is the memory address of your character array. To print the character array, update the format to %s.

Resources