LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 10-21-2007, 11:23 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Rep: Reputation: 16
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.
 
Old 10-21-2007, 11:47 AM   #2
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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 11:52 AM.
 
Old 10-21-2007, 11:56 AM   #3
reverse
Member
 
Registered: Apr 2007
Distribution: Gentoo
Posts: 337

Rep: Reputation: 30
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!?
 
Old 10-21-2007, 12:14 PM   #4
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Original Poster
Rep: Reputation: 16
Thank you for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read HTML or TXT file and output the data? koolkicks311 Programming 1 04-21-2007 12:13 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 06:11 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 08:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 05:16 AM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 09:33 AM


All times are GMT -5. The time now is 12:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration