473,395 Members | 1,706 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.

Return File Contents as String

Hello,

I am completely new to C programming. Below code is that I gathered from Google searches and maybe incorrect.

I am trying to code a function which will read a file on system and return its content back as string. Code is below.

Expand|Select|Wrap|Line Numbers
  1. char * readtxt(){
  2.     FILE * fptr;
  3.     char  c;
  4.     static char txt[30];
  5.     int len=0;
  6.  
  7.     fptr = fopen("C:\\Users\\Test\\Desktop\\Dev\\test.txt", "r");
  8.  
  9.     while(1){
  10.         c = fgetc(fptr);
  11.         if(c!= EOF) {
  12.             txt[len]=c;
  13.             len++;
  14.         }
  15.         else
  16.             break;
  17.     }
  18.     fclose(fptr);  
  19.  
  20.     return txt;  
  21. }
I suppose txt variable is pointer. But I need to return the file content as string so the function structure should look like

Expand|Select|Wrap|Line Numbers
  1. returnType function(){
  2. return "File Contents as String";
  3. }
How can I achieve this please?

Cheers
Oct 1 '14 #1
2 1456
weaknessforcats
9,208 Expert Mod 8TB
I presume this is a text file and not a binary file. That is, you can read the data in a text editor like notepad.exe or vi? Yes?

Assuming that's correct then you want to read data until you reach a newline character (\n). You can do this a character at a time as in your code but you can also use the system's fgets function to read the data into a char array. This function will read characters until is reaches the \n which is changed to \0 to provide the null terminator for the string.

Expand|Select|Wrap|Line Numbers
  1. fgets(txt, 30, fptr);
and you are done.

Note that the source code for all C library functions is available online so you can see how fgets works.

If you have to read character-by-character as a class exercise, then use the loop you have but stop when you see the \n, change the character to \0, append it to your buffer, and stop the loop.
Oct 1 '14 #2
donbock
2,426 Expert 2GB
Are you sure that every line in the input file will fit in a 30-entry array? Don't forget to take into account the extra character (null terminator) added by fgets.

If you read character-by-character then the code in the loop also has to protect against writing past the end of the txt array. You should have explicit protection to keep from writing past the end of the array even if you are certain the input file is always small enough to fit.
Oct 1 '14 #3

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

Similar topics

0
by: Sriram | last post by:
Hi, I have wriiten a program to transfer a binary file. After transfer. The file contents in the destination machine is getting changed. Please Help. Please advice if i am doing any...
8
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
1
by: Michael McGarrigle | last post by:
I would like to send the contents of a file using xp_sendmail however I do not want the file contents to be an attachment. I have no problem sending the file as an attachement. Can anybody give me...
2
by: blip | last post by:
Is this acceptable ? It seems too easy and too simple... #include<iostream> #include<fstream> #include<cstdlib> #include<string> struct Person{ char name ; int age ;
2
by: jmazzi | last post by:
Hey everyone, Im looking to write a program that will read the file contents of a file into an array to use later. Ive searched through this list and seem to only find over bloated programs that...
4
by: igotyourdotnet | last post by:
Currently I have a asp.net 1.1 web page that uploads text file contents into SQL. it reads the files line by line and then inserts the data. I want to convert this web into 05. I was wondering if...
2
by: lab3terch | last post by:
I am a student finishing a class in Visual Basic, and would like some assistance with a program I am working on. I need to read the contents of a file (showing makes of cars on one line and a daily...
6
ak1dnar
by: ak1dnar | last post by:
Hi, I have created Sub in VB.net application, Which reads a text file contents. Sub IPsetter() Dim fileName As String Dim realIParray As String() = Nothing Dim...
0
by: Chris Brooks | last post by:
Jean-Paul Calderone wrote: Actually, I want the contents of the tar uncompressed and untarred. So, for example, I might be working with image data that has to go through several...
15
by: waqasahmed996 | last post by:
hi to all i am trying to get contents of a file in php.my code is $filename = $filepath; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle);...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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.