473,399 Members | 3,401 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,399 software developers and data experts.

getting integers from unknown number of lines from a file.

hi, im new (as you're going to painfully aware of in a minute) to programming and was wondering if anyone could help with this little problem:

i am trying to read a number of values which are separated by newlines, and then manipulate each number until a number =<0 is observed.
is there a way i can use something like fgets() to read the line, then once the manipulation is over, print the result and for the process to be repeated with the number from the next line down in the file?
guessing this would mean id use a "while (integer in question >0), do manipulation and repeat" kinda loop. errr... yeah. the problem is i cant convert my crazy words into a language.

here is what i have so far:

do
{
puts("enter filename");
scanf("%s",name);
FILE*ft;//designates the title ft to the file
ft = fopen(name,"r");//opens the file
if( NULL !=ft)//if the file isnt NULL, then:
{
char filetemps[600];
fgets (filetemps , 600 , ft);//reads the first line only.
puts (filetemps);//just to check it was reading it.
{

else//if the file is NULL prints error statement
{
printf("\nCannot open, please try again %s\n", name);
}//ends else

printf("done\n");
return 2;
}while(name[0]!='z');//ends do

a sample input would be:
294
576
575
-1

thanks for any help you might give me!
May 6 '07 #1
1 2059
Ganon11
3,652 Expert 2GB
First thing I see is that return statement right before you end your do...while loop. Since that's not in any brackets or anything, it's going to end the program without executing any additional times. In other words, the loop will be nullified. Also, you have your filename retrieval inside this loop - meaning you'll be opening a file every time the first number is greater than 0.

You should get the name of the file first, before the loop. Once you have the file opened, then you can use a loop to start reading values. Since fgets retrieves info as a string, you may wish to use fscanf so you can directly access the data as an integer.
May 6 '07 #2

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

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
4
by: Zephyr . via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Zephyr . hey, i got trouble getting integers out of a plain text file. i dont want to use binary files, just plain text files...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
6
by: maheshmadhavan | last post by:
i want to write a C pgm that reads integers from the file which contain unknown no. of integers in each of unknown no. of lines. i want to execute the ogm in linux.
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. ...
4
by: omono84 | last post by:
I know that this should be rather simple but i seem to be missing a step to get it to work. and have been unable to find a solution on the net. The aim is that I click on the open button to find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.