LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 10:23 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

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, 10:47 AM   #2
b0uncer
LQ 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 10:52 AM.
 
Old 10-21-2007, 10: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, 11:14 AM   #4
xeon123
Member
 
Registered: Sep 2006
Posts: 374

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


Reply



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-20-2007 11:13 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 08:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration