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

C->Linux issue with filename construction

140 100+
Hi,

I've some code and its doing some unusual stuff:

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. gstrJobname = "JobOne";
  2.  
  3. strcpy(strFile,"master_");
  4. strcat(strFile,gstrJobname);
  5. strcat(strFile,".dat");
  6.  
The result "should" be "master_JobOne.dat", but it appears that either a newline or a different character is being inserted between the end of the gstrJobname and the file exitension. in the linux directory the file name appears to be:
"master_JobOne?.dat", when I attempt to download this file with an ftp client, it appears as two seperate files "master_JobName" and ".dat"

Any ideas how to perhaps clean up the hidden characters from the end of the gstrJobName. For instance, if I determine the length of that string before I build this file name, is there a way to extract this mystery character?

Thanks,
Sitko.
Nov 6 '07 #1
10 1646
weaknessforcats
9,208 Expert Mod 8TB
How is strFile defined??

Are you overruning the array bound?? It looks like it needs to be an array of at least 18 chars.
Nov 6 '07 #2
sitko
140 100+
How is strFile defined??

Are you overruning the array bound?? It looks like it needs to be an array of at least 18 chars.
char strFile[200];

gstrJobname is read in from an input file. it is also specified as char gstrJobname[200];

I read it in with this line:
Expand|Select|Wrap|Line Numbers
  1. fgets(Jobname, 200, filePtr);
  2. //and later move it to gstrJobname with:
  3. strcpy(gstrJobname,Jobname);
  4.  
It was my belief that fgets would cut off the trailing newline character, could this be the source of the offending hidden character?

If so, how do I get rid of it?

Thanks,
sitko.

ps I would post more of my code, but I've signed a NDA, and can't legally show more than this...
Nov 6 '07 #3
sitko
140 100+
ah...I've just reread the description of fgets and I was mistaken, it includes the newline at the end of the array it reads to.

I guess my followup question would be, how do I read a line of data from a data file, into a string (char array), and leave off the ending newline? Is it posible to use 'gets' instead of fgets? Or even sprintf?

I've been reading up on these, and can find no loop-hole...

Thanks,
Sitko.
Nov 6 '07 #4
sitko
140 100+
I've figured out a potential solution...although I'd still prefer it if someone came up with something better.

Using the code:
Expand|Select|Wrap|Line Numbers
  1. char tempStr[200];
  2.  
  3. strncpy(tempStr,gstrJobname,(strlen(gstrJobname))-1);
  4. strcpy(gstrJobname,tempStr);
  5.  
Seems like it does the trick.
gstrJobname has "JobName\n" in it.
strncpy will copy the first X characters where X = the length of the string (minus 1), and put that into tempStr.
Then I reset gstrJobname = tempStr. and its good to go.

Feels fudgy to me, if anyone can offer something more robust...otherwise thanks for your help,
Brad.
Nov 6 '07 #5
Your solution looks okay to me. The only thing I might do differently is subtract the length of an EOL where an EOL is "\n" on some systems and "\r\n" on others.
Nov 6 '07 #6
primeSo
35
Read a line from a data file without reading the newline character ?

I suggested using fscanf();

Expand|Select|Wrap|Line Numbers
  1. char tempArray[20];
  2.  
  3. fscanf(fp, "%[^\n], tempArray);
  4.  
  5.  
%[^\n] means read in everything except new line character "\n" and store in tempArray via file pointer i.e. fp
Nov 7 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
The reason fgets() retains the \n is so you can do an fputs() without worrying aout the end of line character.

gets() is for stdin only. There the \n is the user's enter key so that is converted to \0 in the receive buffer.

You can use fscanf() if job name is only one word.

Your trick of strlen(gstrJobname))-1 only works if the character following the \n is a \0. That's an assumption. I wouldn't do that.

Instead, I would use strchr to get the location of the \n and then set that location to \0.
Nov 7 '07 #8
primeSo
35
Read a line from a data file without reading the newline character ?

I suggested using fscanf();

Expand|Select|Wrap|Line Numbers
  1. char tempArray[20];
  2.  
  3. fscanf(fp, "%[^\n], tempArray);
  4.  
  5.  
%[^\n] means read in everything except new line character "\n" and store in tempArray via file pointer i.e. fp
Nov 7 '07 #9
sitko
140 100+
Read a line from a data file without reading the newline character ?

I suggested using fscanf();

Expand|Select|Wrap|Line Numbers
  1. char tempArray[20];
  2.  
  3. fscanf(fp, "%[^\n], tempArray);
  4.  
  5.  
%[^\n] means read in everything except new line character "\n" and store in tempArray via file pointer i.e. fp
Thanks, I did some more research, on fscanf and it skips "\n" from the get go...
Nov 7 '07 #10
sitko
140 100+
Later when doing the windows version, I went ahead and got rid of all the fgets, in favor of fscanfs, and it worked like a charm...

Thanks,
Sitko.
Nov 12 '07 #11

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

Similar topics

10
by: Nick Mudge | last post by:
I currently have Windows XP, but I am thinking of changing my operating system to linux because all i want to do is web development with PHP, MySQL and Apache. Does anybody recommend the best...
10
by: matt | last post by:
I have this code, works perfectly on Windows server, but now i'm trying to run it on a Linux server, the form submits, i get no errors, but the photo doesnt upload, and the caption file doesnt...
1
by: JW | last post by:
Hi, I am trying to see if I can display an animated .gif file on a Java application (that is, I do not want to have to show a series of static images to simulate animateion; I just want to...
1
by: Parzival | last post by:
I have a program which works well on under Windows, which I am trying to get to work under Linux (Mandrake 9.1) It accesses a binary file with mmap. Under Windows, the file maps and reads withut...
6
by: Scott Simpson | last post by:
What is a good tool for running XPath queries on Linux? I have the O'Reilly XPath book and I'm using XPath Visualizer on Windows and that seems to work fine, but I'm looking for something on Linux....
22
by: Matthias Braun | last post by:
Help! I am using SuSE 9.3, php5 and Apache2 on Linux. I try to write files to the hard drive using the following command: $filename = "test.txt"; if (!file_exists($filename))...
57
by: B.r.a.d.l.e.y | last post by:
A client has moved a back-end data file to a Linux machine instead of Windows. It now will only let one user log in at a time and gives some sort of "admin file not found" error. My first...
27
by: gmtonyhoyt | last post by:
I need assistance coming up with a clean way to handle filename extensions with my application. While I can come up with several ways of doing so on my own, I felt perhaps it would be worth...
6
by: Franz Steinhaeusler | last post by:
Hello NG, a little longer question, I'm working on our project DrPython and try fix bugs in Linux, (on windows, it works very good now with latin-1 encoding). On Windows, it works good now,...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...

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.