473,396 Members | 1,773 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,396 software developers and data experts.

Reading from input file for C

i am required to code in C but i have limited knowledge.
Can i read a certain number of lines in a input file, then stop reading and do some functions, then continue to read from the point where i stop by calling a method to read again?
Mar 27 '07 #1
5 1985
RedSon
5,000 Expert 4TB
i am required to code in C but i have limited knowledge.
Can i read a certain number of lines in a input file, then stop reading and do some functions, then continue to read from the point where i stop by calling a method to read again?
Yes. Your input stream or input buffer will remember where it is as long as you have set up a pointer to the data.
Mar 27 '07 #2
Yes. Your input stream or input buffer will remember where it is as long as you have set up a pointer to the data.
How do i actually do it? At the moment, i only know how to read the first line from the input file

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int input_character;
  6.   FILE *my_stream;
  7.   char my_filename[] = "command.txt";
  8.   // char my_string[100];
  9.    int bytes_read;
  10.   int nbytes = 100;
  11.   char *my_string;
  12.  
  13.   my_stream = fopen (my_filename, "r");
  14.  
  15.   my_string = (char *) malloc (nbytes + 1);
  16.   bytes_read = getline (&my_string, &nbytes,my_stream);
  17.  
  18.   /* Close stream; skip error-checking for brevity of example */
  19.   fclose (my_stream);
  20.  
  21.   printf ("%s", my_string);
  22.  
  23.   return 0;
  24.  
  25. }
  26.  
Mar 27 '07 #3
RedSon
5,000 Expert 4TB
How do i actually do it? At the moment, i only know how to read the first line from the input file

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int input_character;
  6.   FILE *my_stream;
  7.   char my_filename[] = "command.txt";
  8.   // char my_string[100];
  9.    int bytes_read;
  10.   int nbytes = 100;
  11.   char *my_string;
  12.  
  13.   my_stream = fopen (my_filename, "r");
  14.  
  15.   my_string = (char *) malloc (nbytes + 1);
  16.   bytes_read = getline (&my_string, &nbytes,my_stream);
  17.  
  18.   /* Close stream; skip error-checking for brevity of example */
  19.   fclose (my_stream);
  20.  
  21.   printf ("%s", my_string);
  22.  
  23.   return 0;
  24.  
  25. }
  26.  
What if you put a loop around the getline statement? What if you didn't close the stream but instead did some functions then called getline again? There is nothing in the C language or steam class that says you have to read one line, then close the stream then do some functions with the data you read.
Mar 27 '07 #4
What if you put a loop around the getline statement? What if you didn't close the stream but instead did some functions then called getline again? There is nothing in the C language or steam class that says you have to read one line, then close the stream then do some functions with the data you read.
Thanks for the reply.

1) if my file contains multiple lines, how do i loop and print out each line until the end of file. I tried using read (bytes_read!=-1), but it gives me error.

2) I do not want to put a while loop bcos the function that will be running will invoke the readFile only when necessary..
Something like function A want to read 3 lines, then do it processing, then it want to read 4 lines.

Thks again
Mar 27 '07 #5
RedSon
5,000 Expert 4TB
Thanks for the reply.

1) if my file contains multiple lines, how do i loop and print out each line until the end of file. I tried using read (bytes_read!=-1), but it gives me error.

2) I do not want to put a while loop bcos the function that will be running will invoke the readFile only when necessary..
Something like function A want to read 3 lines, then do it processing, then it want to read 4 lines.

Thks again
1) There must be a way to tell if the stream has more data in it to read or if it has reached the end of file. You will have to read up on the documentation of the stream code or getline to see what the end of file result is.

2) Then put the readFile inside an if statement and then if a certain condition is met then you can read the file again.
Mar 27 '07 #6

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

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
18
by: Michael | last post by:
Hi, I moved to c++ from c, and wanted to know what the best way to read data from files is in c++. Any thoughts? fscanf() is possible but fairly painful! Regards Michael
3
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
9
by: paczkow | last post by:
Dear Python Community, I am an engineering and I am experiencing some trouble. Having output data from other software I want to use it. To achieve this I decided to use Python since this...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
4
by: tushar.saxena | last post by:
Hi, I'm trying to read a file using the istearm class (I cant use ifstream since the input might be a file or it might be stdin). istream *input; // Add checks for file name here, else use...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.