473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Read file in C

Hi guys,

I have a problem in reading a file content which r in tab separated value
formate eg.
MODULE AD0 AD1 AD2 AD3 AD4 AD5 AD6 AD7
4011 +001.10 -001.00 +000.11 +000.11 -001.11 ~ ~ ~
4012 +001.10 -001.00 +000.11 +000.11 -001.11 ~ ~ ~
4013 +001.10 -001.00 +000.11 +000.11 -001.11 ~ ~ ~
4014
from above table i have to read 1st column like.
MODULE
4011
4012
4013

i have tried some code which is read the content & print it in desired formate
BUT THE MAIN PROBLEM IS WHEN I TRY TO STORE IN CHAR * ARRAY
IT GET STORE NEXT VALUE WHICH IS POINTED BY FILE POINTER

Code on Linux system:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<fcntl.h>
  4. #include<sys/stat.h>
  5.  
  6. #define LINE_LENGTH  200    
  7. #define ESC          27
  8.  
  9.    int i,j,l = 0;
  10.  
  11.    main()                                                         
  12.    {
  13.  
  14.         FILE *fp;                                                                               
  15.  
  16.         char list[LINE_LENGTH];
  17.  
  18.         char *choices[6];
  19.  
  20.         int num;        
  21.  
  22.        fp=fopen("../pankaj/c1.txt","r");
  23.  
  24.        printf("\n Enter the No.of Modules :");
  25.  
  26.         scanf("%d",&num);
  27.  
  28.         system("clear");  
  29.  
  30.         int k =0;
  31.  
  32.         while(fgets(list,LINE_LENGTH,fp) &&  num >= 0 )
  33.          {
  34.  
  35.                   char *result1=NULL;
  36.                   char *sptr=list;
  37.               //   result1 = strtok_r(list,"\n",&sptr);
  38.                   result1=strsep(&sptr,"\n");
  39.  
  40.                   while(result1!=NULL)            
  41.                     { 
  42.                      int len;
  43.                      len  =strlen(result1);
  44.  
  45.                      result1[len]='\0';
  46.  
  47.                      choices[k]=result1;
  48.  
  49.  
  50.                      printf("\n%s ",result1);    
  51.                      printf(" %s\n", choices[k]);     
  52.  
  53.  
  54.                      result1= strtok(NULL,"\n");
  55.  
  56.                          k=k+1;
  57.                    }                        
  58.  
  59.  
  60.  
  61.                      num--;          
  62.  
  63.          }
  64.  
  65.  
  66.         for(l=0;l<=5;l++)
  67.             {
  68.              printf("\n%s\n",choices[l]) ; 
  69.             }
  70.  
  71.     fclose(fp);
  72.  
  73.   }
  74.  
In above code the statement printf("\n%s ",result1);
printf(" %s\n", choices[k]);

gives desired output but when i try to store in char * choices[ ]
and then try to print out of while loop it get printed value as ......
4014
which is not a desired o/p

So my question is why this array ptr stores some other value??????????
plz help me I m new in C so if is there any mistake plz forgive me..............

I HOPE u will Reply me as soon as possible......
May 3 '07 #1
1 2582
mac11
256 100+
Your getting strange results because your only pointing to list[] with choices[]. The next time through the loop list changes (the contents of list are changed) but the pointers in choices[] still point to the same location from list[] (but that location now contains something else).

Try copying from list[] to choices[] - but you will first have to give choices[] some memory. Either use malloc() to allocate on the fly under choices[k] or just declare choices with memory to begin with (choices[6][128] - or whatever is enough), then use strcpy() to copy the contents of list[] before they change
May 3 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.