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. |
| 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-21-2007, 10:23 AM
|
#1
|
|
Member
Registered: Sep 2006
Posts: 342
Rep:
|
Read a txt file in C
hi,
I start to hate C, because I don't understand what's going on inside the primitives functions.
Why this example, doesn't read a txt fil.e The temp.txt exists and it's in the same path has the C file.
Code:
void redirect_in(char* cmd, char* file)
{
int i;
int thisin;
int pid[2];
// unsigned char *buffer;
char *data;
int fileLen;
FILE* fd;
fd = fopen("./temp.txt", "r");
// pipe( pid );
//int fint = fork();
//if ( fint == 0 )
// {
//dup2( fd, fileno( stdin ));// to read
//dup2( pid[1], fileno( stdout ));// to write
fseek(fd, 0, SEEK_END);
fileLen=ftell(fd);
printf("%d\n", fileLen);
data = (char*) malloc(fileLen + 1);
//fread(buffer, fileLen, 1, fd);
//while (feof(fd) == 0)
// {
// fgets(data, 10, fd); /* Read next record */
while(fgets(data, 80, fd) != NULL)
{
/* get a line, up to 80 chars from fr. done if NULL */
sscanf (data, "%s");
/* convert the string to a long int */
printf ("%s\n", data);
}
Any help?
The C is killing me, because I don't understand what's going on.
|
|
|
|
10-21-2007, 10:47 AM
|
#2
|
|
Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131
Rep: 
|
I'd start off by taking the file reading procedure into it's own .C file (minimal main() to test it), compile it and see if it works or not. When it works, then just move the code into your bigger program..
Also if it's in the same directory, you don't need "./filename.txt", "filename.txt" should do just fine.
EDIT: something like
Code:
#include <stdio.h>
int main(void)
{
FILE* fd;
int c;
fd = fopen("somefile.txt", "r");
while ((c = getchar()) != EOF);
printf("%d", c);
fclose(fd);
return 0;
}
If the above snipplet contains errors, don't blame me, I wrote it off the top of my head and honestly I can't say I'll remember everything 100% correctly.. 
Last edited by b0uncer; 10-21-2007 at 10:52 AM.
|
|
|
|
10-21-2007, 10:56 AM
|
#3
|
|
Member
Registered: Apr 2007
Distribution: Gentoo
Posts: 337
Rep:
|
Code:
while(fgets(data, 80, fd) != NULL)
{
/* get a line, up to 80 chars from fr. done if NULL */
sscanf (data, "%s");
/* convert the string to a long int */
printf ("%s\n", data);
}
What is that "sscanf(data, "%s");" supposed to do? And.. "convert the string to a long int" <-- WTH!?
|
|
|
|
10-21-2007, 11:14 AM
|
#4
|
|
Member
Registered: Sep 2006
Posts: 342
Original Poster
Rep:
|
Thank you for the help.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:47 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|