C - how to open a txt file and then display the data
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In C, you can only use "==" for primitive types (like an int, float or a single character). For a string, you must use a comparison function like "strcmp()".
Here's an example that might help:
Code:
$ cat history.txt
aaa
bbb
ccc
ddd
Code:
$ cat tmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 80
#define HISTORY_FILE "history.txt"
int
main(int argc, char *argv[])
{
if (argc != 2)
{
printf ("USAGE: enter command (ex. \"history\")\n");
return 1;
}
/* if user enters history has a string then display the history txt file. */
if (strncmp (argv[1], "history", 7) == 0)
{
char input[MAX_LINE + 1];
int line_no = 0;
FILE *fp = fopen (HISTORY_FILE, "r");
if (!fp)
{
printf ("ERROR: Unable to open (%s)!\n", HISTORY_FILE);
return 1;
}
while (!feof (fp))
{
if (fgets (input, MAX_LINE, fp))
printf ("%4d %s", line_no++, input);
}
fclose (fp);
}
else
{
printf ("unknown/unsupported command: %s\n", argv[1]);
return 1;
}
return 0;
}
In C, you can only use "==" for primitive types (like an int, float or a single character). For a string, you must use a comparison function like "strcmp()".
Here's an example that might help:
Code:
$ cat history.txt
aaa
bbb
ccc
ddd
Code:
$ cat tmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 80
#define HISTORY_FILE "history.txt"
int
main(int argc, char *argv[])
{
if (argc != 2)
{
printf ("USAGE: enter command (ex. \"history\")\n");
return 1;
}
/* if user enters history has a string then display the history txt file. */
if (strncmp (argv[1], "history", 7) == 0)
{
char input[MAX_LINE + 1];
int line_no = 0;
FILE *fp = fopen (HISTORY_FILE, "r");
if (!fp)
{
printf ("ERROR: Unable to open (%s)!\n", HISTORY_FILE);
return 1;
}
while (!feof (fp))
{
if (fgets (input, MAX_LINE, fp))
printf ("%4d %s", line_no++, input);
}
fclose (fp);
}
else
{
printf ("unknown/unsupported command: %s\n", argv[1]);
return 1;
}
return 0;
}
Thank you, this helped me a lot, I am also stuck on something like if user enters !12...it needs to go into my history text file and then go to line 12 and get the string and have it save to a buffer.
!number - will get the line in the history list that is preceded with that number
!! - get the last line from history text file
!string - will get the last command that begins with that string
!$ -
I am also stuck on something like if user enters !12...it needs to go into my history text file and then go to line 12 and get the string and have it save to a buffer.
If user enters !12, you just need to :
1. Reset your file pointer to the start of file.
2. Increment your file pointer by 12
3. Read the string character by character till you get a white space.
4. Display it.
Suggestion :
It is very important for you to get a hold on the file pointer manipulations.
Can somebody point me in the right direction. Lets say you want line 4 from text history file..how would i go about going into text file and getting that line and saving it to a variable or pointer.? I have read what anishakaul suggested but i still can't figure it out.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.