473,468 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Print New Line to File in C

2 New Member
Good Evening,

I'm trying to print information to a file with some basic information about a group of people, but when I use the fprintf function and add the \n or \r to the end of the format, the information is not printed on a new line in the text document I have opened. It is instead printing the information without spaces, on the last line of the document to the right of the old information.

I'm running on Windows Vista Ultimate with cygwin in C.

This isn't a homework assignment, I promise.

One other thing, is there any way to scan the number of lines in a text file? I currently have a function that just scans in every line until it reaches the EOF and a variable keeps count of the number of times of a successful fscanf.

Cheers,

-Dirk
May 5 '08 #1
2 8691
oler1s
671 Recognized Expert Contributor
but when I use the fprintf function and add the \n or \r to the end of the format
So i assume this file is opened in binary mode? You may have to use a combination of \r\n.

One other thing, is there any way to scan the number of lines in a text file? I currently have a function that just scans in every line until it reaches the EOF and a variable keeps count of the number of times of a successful fscanf.
Yeah, that's pretty much the idea. A line is denoted by the number of newlines there are. So you basically read in every line until you run out...

until it reaches the EOF
Hope you didn't condition on feof or something like that.
May 5 '08 #2
Dirkle
2 New Member
So i assume this file is opened in binary mode? You may have to use a combination of \r\n.
I don't believe it is opened in binary mode. I just used
Expand|Select|Wrap|Line Numbers
  1.     FILE *infilefp;
  2.     int sr, i;
  3.  
  4.     /* Open file and read in all information */
  5.  
  6.     infilefp = fopen("empdata.txt", "r");
  7.     if(infilefp == NULL){
  8.         printf("Error, file not found!\n\n");
  9.         exit(1);
  10.     }
But the \r\n worked brilliantly, thank you.

Yeah, that's pretty much the idea. A line is denoted by the number of newlines there are. So you basically read in every line until you run out...
Would it be better to use fscanf and some variable that gets fscanf's return and to check for an EOF? Or use fgets for each line, then sscanf?

Hope you didn't condition on feof or something like that.
I just did an fscanf like I described above, but I think I'm going to need to change it to use fgets instead. Right now, the program freezes with any more than two lines in the text file.

Cheers,

-Dirk
May 5 '08 #3

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

Similar topics

0
by: Sure | last post by:
I my php script I call a perl script that perl script will create a log file From the php script I want to read the log file and write the output to the browser. This operation has to carried out...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
7
by: Leonel Gayard | last post by:
Hi all, I had to write a small script, and I did it in python instead of shell-script. My script takes some arguments from the command line, like this. import sys args = sys.argv if args ==...
7
by: Ron | last post by:
Hi All, Is it possible to have Access print a report, identical to one that would print to a printer, only print to a "standard" text file? I can't find it in help and when I try to just print...
3
by: AWasilenko | last post by:
I'm still in the process of learning python via a handful of books I bought. One book I am reading just introduced Base Class Methods. I found that I needed more understanding on this concept and...
7
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
Who could explain the follow issue ? ¦¤ Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'gbk' codec can't encode character u'\x80' in position 0: il...
3
by: Max58kl | last post by:
Trying to access data and print it to the screen using Perl Builders I/O Window -------------------------------------------------------------------------------- Hi I am using a program called...
3
by: itdaddy | last post by:
hey perl gurus! i am new to this forum cause i need help. I have done many scripts. but i want to use perl to do this: What I want to do is this. I have a QRP file that I can convert to a txt...
3
by: lilly07 | last post by:
I am new to perl. I would like to open a file and print 2,6,10,14.. etc lines of that file content. How to do it? I amm able to open a file and print the contetnts as below: #!usr/bin/perl ...
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
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...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.