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.
C - how to open a txt file and then display the data
I am new to c, can somebody help me, i want to open a txt file and then read line by line and display data with first line and label it 1 and then incremented by 1 for each additional line.
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
and think twice before making a first post to the thread because unknowingly you may take the thread off the zero-reply list without a constructive post.
I am new to c, can somebody help me, i want to open a txt file and then read line by line and display data with first line and label it 1 and then incremented by 1 for each additional line.
It is always better to search Google before posting here.
thanks, that helped me a lot
I am stuck on something else now, i tried google and could not find nothing.
Lets say if i type display in command prompt it should display my txt file, how do i compare the input from user input and then do my display of txt statement after that. Like for example i tried this but it doesn't work.
char input[10240];
if ( fgets(input, sizeof(input), stdin)) == "display" )
....
display my txt file now.
Lets say if i type display in command prompt it should display my txt file,
Is it compulsory for you to use the display command ?
Quote:
Originally Posted by UltimateDesi
how do i compare the input from user input and then do my display of txt statement after that. Like for example i tried this but it doesn't work.
Do you mean here that you want to take some input from the user and then compare it with your txt file and then check whether they are identical ? Kindly clarify the question !
Is it compulsory for you to use the display command ?
Do you mean here that you want to take some input from the user and then compare it with your txt file and then check whether they are identical ? Kindly clarify the question !
Purpose of this, is same has gnu history command, but i need make my own that does the same. when you enter history it displays every command you have enter in shell prompt.
i have application, where i can enter commands(Linux), so whatever i type is stored in this txt file, if i enter for example displaytxt, it should open my txt file and display everything i have done in this application.
Last edited by UltimateDesi; 10-13-2010 at 02:31 AM.
not compare with txt file, i have application, where i can enter commands(Linux), so whatever i type is stored in this txt file, if i enter for example displaytxt, it should open my txt file and display everything i have done in this application.
All I can understand by this is that you have a txt file which contains various Linux commands, now you want to read a command from the text file and execute it ?
Sorry the question is still not very clear. Can you demonstrate this with an ACCURATE EXAMPLE ?
Purpose of this, is same has gnu history command, but i need make my own that does the same. when you enter history it displays every command you have enter in shell prompt.
Does this mean you have to implement your own history command ?
So this application: you can enter any commands you want for example let's say you enter ls, pwd, cd etc. It takes these commands and appends them to a text file called history and then let's say you enter history in application, so now it should open that text where commands were appended and display them on screen.
So this application: you can enter any commands you want for example let's say you enter ls, pwd, cd etc. It takes these commands and appends them to a text file called history and then let's say you enter history in application, so now it should open that text where commands were appended and display them on screen.
This means you need to implement your own shell, which takes commands like ls, pwd etc., stores them in *some file* and when you execute that file it should display those commands.
This means you need to implement your own shell, which takes commands like ls, pwd etc., stores them in *some file* and when you execute that file it should display those commands.
Do you exactly need this ?
File should be execute if and only if in this shell application you enter string for example "history". And then open text file and display on screen.
It works same way has gnu history. It will display history if and only if you enter string history.
I think i am making this harder then it should be, but for some reason i can't this to work, so i have a while loop that takes a user input in shell application and keeps appending all the commands you type into history.txt but if you type history it should bring up that history txt file and now display whats store in txt file(which is all the commands you been typing)
My problem is the if statement that tests if the user input is history.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char input[10240];
while (fgets(input, sizeof(input), stdin)) {
if ( input == "history") //if user enters history has a string then display the history txt file.
{
FILE *fopen(), *fp;
int c;
fp = fopen("history.txt","r");
c = getc(fp) ;
while (c!= EOF)
{
putchar(c);
c = getc(fp);
}
fclose(fp);
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.